在微信小程序的一个页面中开启多个websocket连接

本文详细介绍了在微信小程序中如何正确实现多个WebSocket连接的方法。通过对比错误的连接方式,阐述了使用SocketTask对象分别管理每个连接的重要性。文章提供了正确的代码示例,展示了如何为每个连接设置独立的事件监听器,确保数据的准确收发。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用以下代码的情况下,只能开启一个websocket连接,如果开启了多个数据就不对了

var socketTask1 = wx.connectSocket({//打开websocket连接
      url: 'wss://test.com/ws1',
      success: function (resConnect) {//打开连接成功
        console.log(resConnect)
        wx.onSocketOpen(function (resOpen) {
          console.log(resOpen)
          wx.sendSocketMessage({//发送消息
            data: JSON.stringify({
              number: '123'
            }),
            success: function (resSend) {
              console.log(resSend)
            },
            fail: function (resSendError) {
              console.log(resSendError)
            }
          })
        })
        wx.onSocketMessage(function (resMessage) {//接收返回消息
          console.log(resMessage.data)
        })
        wx.onSocketError(function (resError) {//出现错误
          console.log(resError)
        })
        wx.onSocketClose(function (resClose) {//连接关闭
          console.log(resClose)
        })
      },
      fail: function (resConnectError) {//打开连接失败
        console.log(resConnectError)
      }
    })

var socketTask2 = wx.connectSocket({//打开websocket连接
      url: 'wss://test.com/ws2',
      success: function (resConnect) {//打开连接成功
        console.log(resConnect)
        wx.onSocketOpen(function (resOpen) {
          console.log(resOpen)
          wx.sendSocketMessage({//发送消息
            data: JSON.stringify({
              number: '234'
            }),
            success: function (resSend) {
              console.log(resSend)
            },
            fail: function (resSendError) {
              console.log(resSendError)
            }
          })
        })
        wx.onSocketMessage(function (resMessage) {//接收返回消息
          console.log(resMessage.data)
        })
        wx.onSocketError(function (resError) {//出现错误
          console.log(resError)
        })
        wx.onSocketClose(function (resClose) {//连接关闭
          console.log(resClose)
        })
      },
      fail: function (resConnectError) {//打开连接失败
        console.log(resConnectError)
      }
    })

如果要同时开启多个websocket,可以这样写:

    var ws1 = wx.connectSocket({//打开websocket连接
      url: 'wss://test.com/ws1',
      success: function (resConnect) {//打开连接成功
        // console.log(resConnect)
        
      },
      fail: function (resConnectError) {//打开连接失败
        // console.log(resConnectError)
      }
    })
    ws1.onOpen(function(res){
      if (ws1.readyState === 1){
        ws1.send({
          data: JSON.stringify({
            number: '123',
          }),
          success: function (resSend) {
            // console.log(resSend)

          },
          fail: function (resSendError) {
            // console.log(resSendError)
          }
        })
      }
    })

    ws1.onMessage(function (data) {
      console.log(data.data)
    })

    var ws2 = wx.connectSocket({//打开websocket连接
      url: 'wss://test.com/ws2',
      success: function (resConnect) {//打开连接成功
        // console.log(resConnect)
        
      },
      fail: function (resConnectError) {//打开连接失败
        // console.log(resConnectError)
      }
    })
    ws2.onOpen(function(res){
      if (ws2.readyState === 1){
        ws2.send({
          data: JSON.stringify({
            number: '234',
          }),
          success: function (resSend) {
            // console.log(resSend)

          },
          fail: function (resSendError) {
            // console.log(resSendError)
          }
        })
      }
    })

    ws2.onMessage(function (data) {
      console.log(data.data)
    })

参考文档:https://developers.weixin.qq.com/miniprogram/dev/api/network/websocket/SocketTask.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值