收藏
回答

请问微信小程序UDP连接能直接向指定的外网IP地址或者是域名发起连接吗?

核心代码:

console.log("onLaunch called");
console.log("test udp code begin");
 
const udp = wx.createUDPSocket();
udp.bind();
udp.send({
  address: '写死的IP地址或者域名都不行',
  port: 10000,
  message: 'hello, how are you'
});
console.log("message sent.");
udp.onMessage(function (res) {
  console.log("udp.OnMessage res:", res);
  let unit8Arr = new Uint8Array(res.message);
  let encodedString = String.fromCharCode.apply(null, unit8Arr);
  let message = decodeURIComponent(escape((encodedString)));
  console.log(message);
  console.log(res.remoteInfo);
  that.setData({
    receiveMessage: message,
    fromIp: res.remoteInfo.address
  })
});
console.log("test udp code end");

大家是怎么解决的?难道只能用WEBSOCKET吗?但是我看市面上很多游戏都是用的WEBSOCKET,经常掉线断线之类的事情。难道可靠一点的一个都没有吗?UDP可以自己建立一整套可靠传输机制的。难道微信小程序可靠的只能走HTTPS协议了吗?大家是怎么解决的?

回答关注问题邀请回答
收藏

1 个回答

登录 后发表内容
问题标签