收藏
回答

多次重连WebSocket,之后连接WebSocket

问题模块 框架类型 问题类型 API/组件名称 终端类型 微信版本 基础库版本
API和组件 小程序 Bug wx.onSocketOpen 客户端 7.03,7.04 2.66

- 当前 Bug 的表现(可附上截图)


- 预期表现


- 复现路径


- 提供一个最简复现 Demo


多次重连WebSocket,之后连接WebSocket  ,wx.connectSocket有成功回调,但是wx.onSocketOpen没有被调用,最后返回的错误是errMsg:connect response time out

最后一次编辑于  05-14  (未经腾讯允许,不得转载)
回答关注问题邀请回答
收藏

2 个回答

  • 社区技术运营专员-白柿子
    社区技术运营专员-白柿子
    05-15

    麻烦提供能复现问题的代码片段(https://developers.weixin.qq.com/miniprogram/dev/devtools/minicode.html)

    05-15
    赞同
    回复 3
    • 雪灵
      雪灵
      05-15

      可以帮忙看看吗,急着上线

      05-15
      回复
    • Eric Huang
      Eric Huang
      05-15回复雪灵

      android 还是 iOS?

      05-15
      回复
    • 雪灵
      雪灵
      05-16回复Eric Huang

      安卓,华为手机

      05-16
      回复
  • 等待戈多                      ༽
    等待戈多 ༽
    05-16


    onClose之后WebSocket重新初始化

    05-16
    赞同 1
    回复 3
    • 雪灵
      雪灵
      05-16

      好的,谢谢

      05-16
      回复
    • ''.''
      ''.''
      05-28

      怎么重新初始化

      05-28
      回复
    • 等待戈多                      ༽
      等待戈多 ༽
      05-28回复''.''
      initSocket() {
              let that = this
              that.globalData.localSocket = wx.connectSocket({
                  url: 'wss://xxxx.com'
              })
              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.callback(res)
              })
              that.globalData.localSocket.onError(function (res) {
                  //console.log('readyState=' + that.globalData.localSocket.readyState)
              })
              that.globalData.localSocket.onClose(function (res) {
                  //console.log('WebSocket连接已关闭!readyState=' + that.globalData.localSocket.readyState)
                  that.initSocket()
              })
          },
          //统一发送消息
          sendSocketMessage: function (msg) {
              if (this.globalData.localSocket.readyState === 1) {
                  this.globalData.localSocket.send({
                      data: JSON.stringify(msg)
                  })
              } else {
                  socketMsgQueue.push(msg)
              }
          },


      我的代码片段

      05-28
      回复