This API is used via UDPSocket?wx.createUDPSocket().
This method is used via number UDPSocket.bind(number port).
const udp = wx.createUDPSocket()const port = udp.bind()
This method is used via UDPSocket.close().
This method is used via UDPSocket.connect(Object object).
Attribute | Type | Default value | Required | Note |
address | string | - | Yes | Destination for message transmission |
Port No. | number | - | Yes | Port number for message transmission |
const udp = wx.createUDPSocket()udp.bind()udp.connect({address: '192.168.193.2',port: 8848,})udp.write({address: '192.168.193.2',port: 8848,message: 'hello, how are you'})
This method is used via UDPSocket.onClose(function listener).
This method is used via UDPSocket.offClose(function listener).
const listener = function (res) { console.log(res) }UDPSocket.onClose(listener)UDPSocket.offClose(listener) // The same function object as the listener must be passed in.
This method is used via UDPSocket.onError(function listener).
Attribute | Type | Note |
errMsg | string | Error Message |
This method is used via UDPSocket.offError(function listener).
const listener = function (res) { console.log(res) }UDPSocket.onError(listener)UDPSocket.offError(listener) // The same function object as the listener must be passed in.
This method is used via UDPSocket.onListening(function listener).
This method is used via UDPSocket.offListening(function listener).
const listener = function (res) { console.log(res) }UDPSocket.onListening(listener)UDPSocket.offListening(listener) // The same function object as the listener must be passed in.
This method is used via UDPSocket.onMessage(function listener).
Attribute | Type | Note |
message | ArrayBuffer | The received message. The length of the message must be less than 4096. |
remoteInfo | Object | Sender's address information |
Structure attributes | Type | Note |
address | string | The address of the socket sending the message |
family | string | The protocol family in use, either IPv4 or IPv6. |
Port No. | number | Port No. |
size | number | The size of the message, measured in bytes. |
This method is used via UDPSocket.offMessage(function listener).
const listener = function (res) { console.log(res) }UDPSocket.onMessage(listener)UDPSocket.offMessage(listener) // The same function object as the listener must be passed in.
This method is used via UDPSocket.send(Object object).
Attribute | Type | Default value | Required | Note |
address | string | - | Yes | The address to send the message to. |
Port No. | number | - | Yes | Port number for message transmission |
message | string/ArrayBuffer | - | Yes | Data to be transmitted |
offset | number | 0 | No | The offset of the data to be sent, valid only when the message is of the ArrayBuffer type. |
length | number | message.byteLength | No | The length of the data to be sent, valid only when the message is of the ArrayBuffer type. |
const udp = wx.createUDPSocket()udp.bind()udp.send({address: '192.168.193.2',port: 8848,message: 'hello, how are you'})
This method is used via UDPSocket.setTTL(number ttl).
const udp = wx.createUDPSocket()udp.onListening(function () {udp.setTTL(64)})udp.bind()
This method is used via UDPSocket.write().
const udp = wx.createUDPSocket()udp.bind()udp.connect({address: '192.168.193.2',port: 8848,})udp.write({address: '192.168.193.2',port: 8848,message: 'hello, how are you'})
Was this page helpful?