收藏
回答

多次重连WebSocket,之后连接WebSocket

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

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


- 预期表现


- 复现路径


- 提供一个最简复现 Demo


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

回答关注问题邀请回答
收藏

2 个回答

  • 金华
    金华
    2019-05-16


    onClose之后WebSocket重新初始化

    2019-05-16
    有用 1
    回复 3
    • 雪灵
      雪灵
      2019-05-16

      好的,谢谢

      2019-05-16
      回复
    • 2019-05-28

      怎么重新初始化

      2019-05-28
      回复
    • 金华
      金华
      2019-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)
              }
          },


      我的代码片段

      2019-05-28
      回复
  • 是小白啊
    是小白啊
    2019-05-15

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

    2019-05-15
    有用
    回复 3
    • 雪灵
      雪灵
      2019-05-15

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

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

      android 还是 iOS?

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

      安卓,华为手机

      2019-05-16
      回复
登录 后发表内容