收藏
回答

小程序tcp连接的监听就执行一次但是触发n次就会一次性执行2*(n-1) +1次咋解决?

TCPSocket.onConnect(function callback)多次重新连接会触发很多次,

 _this.globalData.TCPSocket = wx.createTCPSocket()

        _this.globalData.TCPSocket.onConnect((res) => {

            wx.showToast({

                title: '连接成功',

                icon: 'none'

            })

     _this.globalData.connection_status = true

                setTimeout(() => {

                    wx.switchTab({

                        url: '/pages/index/index',

                    })

                }, 1000)


            }

        })

        _this.globalData.TCPSocket.onError((res) => {


            if (res.errMsg == "this socket tcp has alread connected"{

                _this.globalData.TCPSocket.close()

                var ip = wx.getStorageSync('TCP_IP')

                _this.globalData.TCPSocket.connect({

                    address: ip,

                    port: 4899

                })

            } else if (res.errCode == -2{

                wx.showToast({

                    title: '连接失败:' + res.errMsg,

                    icon: 'none'

                })

                _this.globalData.connection_status = false

            } else {

                wx.showToast({

                    title: '连接失败',

                    icon: 'none'

                })

                _this.globalData.connection_status = false

            }

        })

        _this.globalData.TCPSocket.onClose((res) => {

            // wx.showToast({

            //     title: '连接失败' + JSON.stringify(res),

            //     icon: 'none'

            // })

            wx.showToast({

                title: '断开连接',

                icon: 'none'

            })


            _this.globalData.connection_status = false

        })


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

1 个回答

  • 亗
    2022-03-14

    已解决,只有测试环境下会这样

    2022-03-14
    有用 1
    回复 1
    • 虎子哥
      虎子哥
      2022-03-16
      这完全就是没有解决[狗头]
      2022-03-16
      1
      回复
登录 后发表内容