收藏
回答

udpSocket.onMessage预览时只能发送,不能接收?

使用开发者工具调试时,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);

});



最后一次编辑于  11-25
回答关注问题邀请回答
收藏

2 个回答

问题标签