1.在app.js中连接websockt

这样可以始终保持只有一个websockt连接,不会提示错误

App({
  onLaunch: function () {
    wx.connectSocket({
      url: 'wss://wss.shanling.top'
    })
    wx.onSocketClose(function (res) {
      wx.connectSocket({
        url: 'wss://wss.shanling.top'
      })
    })
    
  }
})

2.在每个页面的onShow中监听wx.onSocketMessage(不要在onLoad中)

这样可以保证当前页面对websocket的控制权,解决使用wx.navigateTo打开子页面时,父级页面websocket失效的问题

onShow: function () {
    var that=this;
    wx.onSocketOpen(function (res) {
      that.onopen()
    })
 
    wx.onSocketMessage(function (res) {
      that.onmessage()
    })
}

 

相关文章:

  • 2022-12-23
  • 2021-12-29
  • 2022-12-23
  • 2022-12-23
  • 2021-06-21
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-05-15
  • 2021-04-23
  • 2021-10-08
  • 2022-12-23
  • 2021-09-05
  • 2021-09-05
  • 2022-12-23
相关资源
相似解决方案