电脑开启tcp server
端口5678
小程序代码
handleTcp() {
const tcp = wx.createTCPSocket()
tcp.connect({
address: '127.0.0.1',
port: 5678
})
tcp.onConnect(() => {
console.log("onConnect")
tcp.write('hello, how are you')
})
tcp.onMessage((message, remoteInfo, localInfo) => {
console.log("onMessage")
console.log(message)
console.log(remoteInfo)
console.log(localInfo)
})
tcp.onError((res) => {
console.log(res)
})
setTimeout(function () {
// 3s后,关闭socket
tcp.close(() => {
console.log("close")
})
}, 3000)
}
运行结果
服务端运行日志
网友评论