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' } uni.sendSocketMessage({ data: "{ \"openId\": \"" + openId + "\", \"state\": \"menu\" }" }); setInterval(function () { console.log("heard") uni.sendSocketMessage({ data: "{ \"my\": \"huozhe\", \"state\": \"heard\" }" }); }, 20000); 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 == 'pay'){ //支付信息 if(data.data == 'start'){ vueVM.getWxCode('https://kd.biutag.com:9903/user/weChat_or_ali/' + uni.getStorageSync('openId')) }else if(data.data == 'end'){ vueVM.getWxCode() } } }); } function sendSocketMessage(msg) { if (socketOpen) { uni.sendSocketMessage({ data: msg }); } else { socketMsgQueue.push(msg); } } export default { getWebSocket }