socket配置
在全局配置(根目录/config/index.js)中配置相关sokect接口地址
路径:根目录/config/index.js
eg: 请更换自己的域名和端口号
const LIVESOKECT = 'wss://xxxx:8273/';
域名:xxxx
端口号:8273
使用说明
# /根目录/utils/websocket.js 基于uni-app封装,有断线重连、心跳检测、发送消息等功能
import wsRequest from '@/utils/websocket'
let websocket = new wsRequest(6000)
Vue.prototype.$socket = websocket
// 链接初始化
Vue.prototype.$socket = new wsRequest(6000)
// 断线重连
this.$socket.manualReconnect()
// 监听消息页面
uni.onSocketMessage(res => {
const e = JSON.parse(res.data)
if (e.type !== 'pong') {
// 业务逻辑
}
})
// 发送消息
uni.sendSocketMessage({
data: JSON.stringify({
})
})
// 断开链接
this.$socket.colse()