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 |
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 |
localInfo | Object | Recipient's address information, supported from V2.18.0 onwards. |
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. |
Structure attributes | Type | Note |
address | string | The address of the socket receiving the message |
family | string | The protocol family in use, either IPv4 or IPv6. |
Port No. | number | Port No. |
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 which the message is to be sent. In the base library version 2.9.3 and earlier, it must be an IP address in the same network segment as the local device, or a domain name address within the security domain list; in the base library version 2.9.3 and later, it can be any IP and domain name. |
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().
Was this page helpful?