May 18, 2021 WeChat Mini Program Development Document
Base library 2.7.0 starts to support, and low versions need to be compatible.
Create a UDP Socket instance. Please read the instructions before use.
A UDP Socket instance
Base library 2.7.0 starts to support, and low versions need to be compatible.
A UDP Socket instance that uses the IPv4 protocol by default.
Bind an available port randomly assigned by the system, or bind a specified port number
Base library 2.9.0 starts to support, and low versions need to be compatible.
Specify the port number to bind, and return the available ports randomly assigned by the system without passing
The port number that was successfully bound
const udp = wx.createUDPSocket()
const port = udp.bind()
Closing a UDP Socket instance is equivalent to destroying it. A fter closing, the UDP Socket instance can no longer send messages, and each call to UDPSocket.send triggers an error event, and the message event callback function is no longer executed. W hen a UDPSocket instance is created, it is strongly referenced by Native to ensure that it is not GC. After UDPSocket.close, the strong reference to it is lifted and the UDPSocket instance follows the GC.
Cancel the listening shutdown event
Close the callback function for the event
Cancel listening for error events
The callback function for the error event
Cancel the event that began listening for packet messages
A callback function that starts listening for events for packet messages
Cancel listening for events that receive messages
The callback function for the event that received the message
Listen for shutdown events
Close the callback function for the event
Listen for error events
The callback function for the error event
Property | Type | Description |
---|---|---|
errMsg | string | The error message |
Listen for events that start listening for packet messages
A callback function that starts listening for events for packet messages
Listen for events that receive messages
The callback function for the event that received the message
Property | Type | Description |
---|---|---|
message | ArrayBuffer | The message received |
remoteInfo | Object | Structured information about the source |
The structure of remoteInfo
Property | Type | Description |
---|---|---|
address | string | The address of the socket that sent the message |
family | string | The protocol family used is IPv4 or IPv6 |
port | number | The port number |
size | number | The size of the message in bytes |
Send messages to the specified IP and port
Property | Type | The default | Required | Description |
---|---|---|---|---|
address | string | Is | The address to send the message. The underlying library 2.9.3 and previous versions can be an IP address with the same segment as the local domain, or a domain name address within the list of secure domain names, and any IP and domain name in the base library 2.9.4 and later | |
port | number | Is | The port number to send the message | |
message | string/ArrayBuffer | Is | The data to send | |
offset | number | 0 | Whether | The offset of the data sent is only valid if the message is of the ArrayBuffer type |
length | number | message.byteLength | Whether | The length of the data sent is valid only if 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'
})