微信小程序UDP通信及编码转换
import Encoder from '../../util/Encoder.js'
onLoad() {
this.udp = wx.createUDPSocket()
this.udp.bind(60000)
},
methods: {
linkUDP() {
this.udp.connect({
address: '192.168.0.87',
port: 50000,
})
},
clickUDP() {
var gbkArray = Encoder.hexstrToArray(Encoder.stringToGbkHexstr('123哈'));
console.log("gbkArray>>" , gbkArray)
let gbk = new ArrayBuffer()
console.log(gbk)
this.udp.write({
address: '192.168.0.87',
port: 50000,
message: gbk
})
},
onUdpMessage: function(res) {
console.log('收到的信息', res);
console.log('onUdpMessage() 接收数据 ' + res.remoteInfo.size + ' 字节:' + JSON.stringify(res, null, '\t'));
var x = new Uint8Array(res.message);
var str = new TextDecoder('gbk').decode(x);
console.log(str)
},
},
mounted() {
this.udp.onMessage(this.onUdpMessage)
this.udp.onError(function(err) {
console.log(err)
})
}
参考连接link. link.
|