小程序UDP通信开发者工具正常使用,真机和预览都不行是什么原因啊?
Page({
data: {
udpSocket: ''
},
sendMessage() {
let ip = '192.168.3.255'
let port = 888
let message = 'hello i am wechat'
this.data.udpSocket.send({
address: ip,
port: port,
message: message
})
},
initUdpSocket() {
this.data.udpSocket = wx.createUDPSocket();
if (this.data.udpSocket === null) {
console.log('暂不支持')
return;
}
this.data.udpSocket.onListening(function (res) {
console.log('监听中Res ===' + res)
})
const locationPort = this.data.udpSocket.bind(888)
this.setData({
'locationUrl.port': locationPort
})
this.data.udpSocket.onMessage(function (res) {
console.log('remoteInfo ===' + res.remoteInfo)
console.log('remoteInfo.size ===' + res.remoteInfo.size)
if (res.remoteInfo.size > 0) {
console.log('UDP接收数据 ' + res.remoteInfo.size + ' 字节:' + JSON.stringify(res, null, '\t'))
let unit8Arr = new Uint8Array(res.message);
let encodedString = String.fromCharCode.apply(null, unit8Arr);
let escStr = escape(encodedString);
let decodedString = decodeURIComponent(escStr);
console.log('str===' + decodedString)
wx.showModal({
title: '提示',
content: decodedString,
success (res) {
if (res.confirm) {
console.log('用户点击确定')
} else if (res.cancel) {
console.log('用户点击取消')
}
}
})
}
})
},
lxt :function(){
this.initUdpSocket()
this.sendMessage()
},
}) 上面的代码开发者工具中可以接收也可以发送,用预览、真机调试、体验版都不能发送和接收。请问是什么原因啊? 谢谢!