小程序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 })