直接上代码了
app.js中声明一个全局对象
globalData: {
localSocket: {},
callback: function () {},
exceptionClose: true
},
// 初始化socket
initSocket() {
let that = this;
that.globalData.exceptionClose=true;
var cookie = '';
var index = 0;
for (var key in that.getCookieSync('cookieKey')) {
if (index > 0) {
cookie += ';'
}
cookie += key + '=' + that.getCookieSync('cookieKey')[key]
index = index + 1;
}
that.globalData.localSocket = wx.connectSocket({
url: api.WebSocketUrl,
header:{'Cookie': cookie}
})
that.globalData.localSocket.onOpen(function (res) {
console.log('WebSocket连接已打开!readyState=' + that.globalData.localSocket.readyState)
while (socketMsgQueue.length > 0) {
var msg = socketMsgQueue.shift();
that.sendSocketMessage(msg);
}
})
that.globalData.localSocket.onMessage(function (res) {
that.globalData.callba