收藏
回答

websocket创建了多于5个链接,还是可以正常运作,和文档描述不符合

框架类型 问题类型 操作系统 工具版本
小游戏 Bug macOS 2.10.0

https://developers.weixin.qq.com/minigame/dev/api/network/websocket/wx.connectSocket.html

let myConnect1 = wx.connectSocket({
      url: 'ws://172.29.36.252:8181',
      success:()=>{
      },
      fail:()=>{
      },
    })
    myConnect1.onMessage((obj)=>{
      console.log("success1 in onMessage"+obj)
    })
    myConnect1.onOpen(()=>{
      console.log("in onOpen1")
    })
    setTimeout(()=>{
      myConnect1.send({
        data:'{"data":"success1"}',
        success:()=>{
          console.log("send1 success")
        },
        fail:()=>{
          console.log("send1 fail")
        }
      })
    },5000)
    

    let myConnect2 = wx.connectSocket({
      url: 'ws://172.16.164.24:8181',
      success:()=>{
      },
      fail:()=>{
      },      
    })
    myConnect2.onMessage((obj)=>{
      console.log("success2 in onMessage"+obj)
    })
    myConnect2.onOpen(()=>{
      console.log("in onOpen2")
    })
    setTimeout(()=>{
      myConnect2.send({
        data:'{"data":"success2"}',
        success:()=>{
          console.log("send2 success")
        },
        fail:()=>{
          console.log("send2 fail")
        }
      })
    },5000)

    let myConnect3 = wx.connectSocket({
      url: 'ws://82.157.123.54:9010/ajaxchattest',
      success:()=>{
      },
      fail:()=>{
      },
    })
    myConnect3.onMessage((obj)=>{
      console.log("success3 in onMessage"+obj)
    })
    myConnect3.onOpen(()=>{
      console.log("in onOpen3")
    })
    setTimeout(()=>{
      myConnect3.send({
        data:'{"data":"success3"}',
        success:()=>{
          console.log("send3 success")
        },
        fail:()=>{
          console.log("send3 fail")
        }
      })
    },5000)

    let myConnect4 = wx.connectSocket({
      url: 'ws://echo.websocket.org',
      success:()=>{
      },
      fail:()=>{
      },
    })
    myConnect4.onMessage((obj)=>{
      console.log("success4 in onMessage"+obj)
    })
    myConnect4.onOpen(()=>{
      console.log("in onOpen4")
    })
    setTimeout(()=>{
      myConnect4.send({
        data:'{"data":"success4"}',
        success:()=>{
          console.log("send4 success")
        },
        fail:()=>{
          console.log("send4 fail")
        }
      })
    },5000)

    let myConnect5 = wx.connectSocket({
      url: 'ws://172.16.164.144:8181',
      success:()=>{
      },
      fail:()=>{
      },
    })
    myConnect5.onMessage((obj)=>{
      console.log("success5 in onMessage"+obj)
    })
    myConnect5.onOpen(()=>{
      console.log("in onOpen5")
    })
    setTimeout(()=>{
      myConnect5.send({
        data:'{"data":"success5"}',
        success:()=>{
          console.log("send5 success")
        },
        fail:()=>{
          console.log("send5 fail")
        }
      })
    },5000)


    let myConnect6 = wx.connectSocket({
      url: 'ws://172.16.153.164:8181',
      success:()=>{
      },
      fail:()=>{
      },
    })
    myConnect6.onMessage((obj)=>{
      console.log("success6 in onMessage"+obj)
    })
    myConnect6.onOpen(()=>{
      console.log("in onOpen6")
    })
    setTimeout(()=>{
      myConnect6.send({
        data:'{"data":"success6"}',
        success:()=>{
          console.log("send6 success")
        },
        fail:()=>{
          console.log("send6 fail")
        }
      })
    },5000)

    let myConnect7 = wx.connectSocket({
      url: 'ws://172.16.169.72:8181',
      success:()=>{
      },
      fail:()=>{
      },
    })
    myConnect7.onMessage((obj)=>{
      console.log("success7 in onMessage"+obj)
    })
    myConnect7.onOpen(()=>{
      console.log("in onOpen7")
    })
    setTimeout(()=>{
      myConnect7.send({
        data:'{"data":"success7"}',
        success:()=>{
          console.log("send7 success")
        },
        fail:()=>{
          console.log("send7 fail")
        }
      })
    },5000)

    let myConnect8 = wx.connectSocket({
      url: 'ws://172.16.160.211:8181',
      success:()=>{
      },

fail:()=>{

      },
    })
    myConnect8.onMessage((obj)=>{
      console.log("success8 in onMessage"+obj)
    })
    myConnect8.onOpen(()=>{
      console.log("in onOpen8")
    })
    setTimeout(()=>{
      myConnect8.send({
        data:'{"data":"success8"}',
        success:()=>{
          console.log("send8 success")
        },
        fail:()=>{
          console.log("send8 fail")
        }
      })
    },5000)


上面是我的代码(我自己在不同ip上起了8个不同的websocket服务),在创建了8个链接之后,我同时通过八个websocket对象发送数据,后台都接受到了数据并回返了相关信息。就是说8个websocket都正常工作了,和文档上描述的最多只支持5个不符合。

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

1 个回答

  • Forever
    Forever
    2021-08-04

    你的手机系统是什么?

    2021-08-04
    有用
    回复 1
    • 陈龙
      陈龙
      2021-08-05
      安卓10
      2021-08-05
      回复
登录 后发表内容