WebSocket在同一个页面初始化两次的问题
问题描述:通过开发工具扫码进入,demo代码 [代码]Page({[代码][代码] [代码][代码]data:{[代码] [代码] [代码][代码]},[代码][代码] [代码][代码]onLoad:[代码][代码]function[代码][代码](options){[代码][代码] [代码][代码]console.info([代码][代码]'ok'[代码][代码]);[代码][代码] [代码][代码]var[代码] [代码]_ = options._;[代码][代码] [代码][代码]console.info(_);[代码][代码] [代码][代码]wx.onSocketOpen([代码][代码]function[代码] [代码](res) {[代码][代码] [代码][代码]wx.sendSocketMessage({[代码][代码] [代码][代码]data:JSON.stringify({[代码][代码] [代码][代码]data:1[代码][代码] [代码][代码]})[代码][代码] [代码][代码]})[代码][代码] [代码][代码]console.log([代码][代码]'打开:'[代码] [代码]+ res);[代码][代码] [代码][代码]});[代码][代码] [代码][代码]wx.onSocketMessage([代码][代码]function[代码] [代码](res) {[代码][代码] [代码][代码]console.log([代码][代码]'收到服务器内容:'[代码] [代码]+ res.data);[代码][代码] [代码][代码]});[代码][代码] [代码][代码]wx.onSocketClose([代码][代码]function[代码] [代码]() {[代码][代码] [代码][代码]console.log([代码][代码]'WebSocket 已关闭!'[代码][代码])[代码][代码] [代码][代码]});[代码][代码] [代码][代码]wx.connectSocket({[代码][代码] [代码][代码]url: `wss:[代码][代码]//seeknow.org/wss`[代码][代码] [代码][代码]})[代码] [代码] [代码][代码]_ && wx.sendSocketMessage({[代码][代码] [代码][代码]data:JSON.stringify({[代码][代码] [代码][代码]data:2222[代码][代码] [代码][代码]})[代码][代码] [代码][代码]})[代码][代码] [代码][代码]},[代码][代码] [代码][代码]onReady:[代码][代码]function[代码][代码](){[代码] [代码] [代码][代码]},[代码][代码] [代码][代码]onShow:[代码][代码]function[代码][代码](){[代码] [代码] [代码][代码]},[代码][代码] [代码][代码]onHide:[代码][代码]function[代码][代码](){[代码] [代码] [代码][代码]},[代码][代码] [代码][代码]onUnload:[代码][代码]function[代码][代码](){[代码] [代码] [代码][代码]},[代码][代码] [代码][代码]onPullDownRefresh:[代码][代码]function[代码][代码](){[代码] [代码] [代码][代码]},[代码][代码] [代码][代码]onReachBottom:[代码][代码]function[代码][代码](){[代码] [代码] [代码][代码]},[代码][代码] [代码][代码]onShareAppMessage:[代码][代码]function[代码][代码](){[代码][代码] [代码][代码]return[代码] [代码]{[代码][代码] [代码][代码]title: `邀请您一起参与专注挑战`,[代码][代码] [代码][代码]desc: [代码][代码]'让我们一起远离手机 专注当下'[代码][代码],[代码][代码] [代码][代码]path: `/pages/test/test?_=${Date.now()}`[代码][代码] [代码][代码]}[代码][代码] [代码][代码]}[代码][代码]})[代码]进入后成功的接收到了服务的反馈,但是通过分享链接再次进入,socket没有断,服务器也能接收到客户端的消息,但是客户端没能接收到信息。 结论:onSocketMessage 方法有bug