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
})
已解决,只有测试环境下会这样