You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
74 lines
1.6 KiB
74 lines
1.6 KiB
var socketOpen = false; |
|
// var socketMsgQueue = 'yskf10001' |
|
function getWebSocket(vueVM){ |
|
let vm = this |
|
|
|
uni.connectSocket({ |
|
url: 'wss://kd.biutag.com:9903/ukeerWechatWebsocketServer.action' |
|
// url: 'ws://localhost:9903/ukeerWechatWebsocketServer.action' |
|
}); |
|
|
|
uni.onSocketOpen(function (res) { |
|
uni.hideLoading(); |
|
uni.showToast({ |
|
title: '网络已连接', |
|
duration: 2000 |
|
}); |
|
let openId = uni.getStorageSync('openId') |
|
if(openId == undefined || openId == ''){ |
|
openId = 'not found' |
|
} |
|
console.log('WebSocket连接已打开!'); |
|
socketOpen = true; |
|
}); |
|
|
|
uni.onSocketError(function (res) { |
|
console.log('WebSocket连接打开失败,请检查!'); |
|
}); |
|
|
|
uni.onSocketClose(function (res) { |
|
uni.showLoading({ |
|
title: '网络断开,重连中...', |
|
mask: true |
|
}) |
|
uni.closeSocket() |
|
console.log('WebSocket 已关闭!重连中...'); |
|
Itime = setTimeout(function () { |
|
vm.getWebSocket(vueVM); |
|
}, 2000) |
|
}); |
|
|
|
uni.onSocketMessage(function (res) { |
|
console.log(res) |
|
let data = JSON.parse(res.data) |
|
if(data.message == 'id'){ |
|
//初始化通道 |
|
vueVM.VXCode('https://kd.biutag.com:9903/weChat/menu/' + data.data) |
|
}else if(data.message == 'login'){ |
|
if(data.data.state == 'success'){ |
|
console.log(data) |
|
let openId = data.data.openId |
|
//登录成功 |
|
vueVM.loginPage(data.data) |
|
} |
|
} |
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
function sendSocketMessage(msg) { |
|
if (socketOpen) { |
|
uni.sendSocketMessage({ |
|
data: msg |
|
}); |
|
} else { |
|
socketMsgQueue.push(msg); |
|
} |
|
} |
|
|
|
|
|
export default { |
|
getWebSocket |
|
} |