使用开发者工具调试时,udpSocket.onMessage可以发送也能接收UDP广播包和指定ip的UDP包。但预览时可以发送成功,接收不到udp包。为什么?
var udp = wx.createUDPSocket();
if(udp == null){
console.log(“udp建立不成功”)
return ;
}
var that = this;
const loactionPort = udp.bind(28266);
var testData = loactionPort;
that.setData({ testData: testData});
udp.onListening(function(res) {
});
udp.onMessage(function(res) {
console.log(res);
var remoteInfo = new Object;
var ipAddr = res.remoteInfo.address;
var port = res.remoteInfo.port;
var str = “”;
for (var i = 0; i < res.message.data.length; i++) {
str = str + String.fromCharCode(res.message.data[i]);
}
console.log(str);
var testData = str;
testData = “str”;
that.setData({testData:testData});
var msg = JSON.parse(str);
remoteInfo.ipAddr = ipAddr;
remoteInfo.port = port;
remoteInfo.msg = msg;
that.setData({
remoteInfo: remoteInfo
});
console.log(that.data.remoteInfo);
});
请具体描述问题出现的流程,并提供能复现问题的简单代码片段(https://developers.weixin.qq.com/miniprogram/dev/devtools/minicode.html)。
https://developers.weixin.qq.com/s/3OHYzbmk7Jd7https://developers.weixin.qq.com/s/ezHylbma7jdK
程序启动后,建立udp,端口8166.点击“文字”发送广播包。用网络调试助手(或其他方式),无论发送JSON广播包或特定ip包,在微信开发工具的模拟器上能接收到数据包,但预览时只能发送,接收不到。