tencent cloud

All product documents
Tencent Cloud Super App as a Service
UDP Communication
Last updated: 2025-03-20 18:01:56
UDP Communication
Last updated: 2025-03-20 18:01:56

createUDPSocket

This API is used via UDPSocket?wx.createUDPSocket().
Feature description: Creates a UDP Socket instance.
Return Value: UDPSocket, a UDP Socket instance.

UDPSocket

Note:
A UDP Socket instance, using IPv4 protocol by default.

.bind

This method is used via number UDPSocket.bind(number port).
Feature Description: Binds to a system-assigned random available port, or bind to a specified port number.
Parameter and Description: number port, No. of specified port to be bound. If this parameter is not passed in, the system randomly assigns an available port.
Return Value: number, the successfully bound port number.
Sample Code
const udp = wx.createUDPSocket()
const port = udp.bind()

.close

This method is used via UDPSocket.close().
Feature Description: Closes the UDP Socket instance, effectively terminating it. After closure, the UDP Socket instance can no longer send messages. Each call to UDPSocket.send will trigger an error event, and the message event callback function will no longer be executed. After a UDPSocket instance is created, it is strongly referenced by Native to ensure it is not garbage collected. After UDPSocket.close, the strong reference is removed, allowing the UDPSocket instance to be subject to garbage collection.

.connect

This method is used via UDPSocket.connect(Object object).
Feature Description: Pre-connects to the specified IP and port, to be used in conjunction with the write method.
Parameter and Description: Object
Attribute
Type
Default value
Required
Note
address
string
-
Yes
Destination for message transmission
Port No.
number
-
Yes
Port number for message transmission
Sample Code
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'
})

.onClose

This method is used via UDPSocket.onClose(function listener).
Feature Description: Monitors closure events.
Parameter and Description: The listener function of closure events.

.offClose

This method is used via UDPSocket.offClose(function listener).
Feature Description: Removes the listener function for closure events.
Parameter and Description: function listener, the listener function passed in by onClose. If this parameter is not passed in, all listener functions will be removed.
Sample Code
const listener = function (res) { console.log(res) }

UDPSocket.onClose(listener)
UDPSocket.offClose(listener) // The same function object as the listener must be passed in.

.onError

This method is used via UDPSocket.onError(function listener).
Feature Description: Monitors error events.
Parameter and Description: Object res parameter, function listener, the listener function for error events.
Attribute
Type
Note
errMsg
string
Error Message

.offError

This method is used via UDPSocket.offError(function listener).
Feature Description: Removes the listener function for error events.
Parameter and Description: function listener, error event listener function, with the following arguments Object res:
Sample Code
const listener = function (res) { console.log(res) }

UDPSocket.onError(listener)
UDPSocket.offError(listener) // The same function object as the listener must be passed in.

.onListening

This method is used via UDPSocket.onListening(function listener).
Feature Description: Monitors events of starting to monitor data packet messages.
Parameter and Description: function listener, the listener function for events of starting to monitor data packet messages.

.offListening

This method is used via UDPSocket.offListening(function listener).
Feature Description: Removes the listener function for events of starting to monitor data packet messages.
Parameter and Description: function listener, the listener function passed in by onListening. If this parameter is not passed in, all listener functions will be removed.
Sample Code
const listener = function (res) { console.log(res) }

UDPSocket.onListening(listener)
UDPSocket.offListening(listener) // The same function object as the listener must be passed in.

.onMessage

This method is used via UDPSocket.onMessage(function listener).
Feature Description: Monitors events of receiving a message.
Parameter and Description: function listener, the listener function for the event that received the message, with the following arguments Object res:
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 of "remoteInfo"
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.

.offMessage

This method is used via UDPSocket.offMessage(function listener).
Feature Description: Removes the listener function for events of receiving a message.
Parameter and Description: function listener, the listener function passed in by onMessage. If this parameter is not passed in, all listener functions will be removed.
Sample Code
const listener = function (res) { console.log(res) }

UDPSocket.onMessage(listener)
UDPSocket.offMessage(listener) // The same function object as the listener must be passed in.

.send

This method is used via UDPSocket.send(Object object).
Feature Description: Sends messages to the specified IP and port.
Parameter and Description: 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.
Sample Code
const udp = wx.createUDPSocket()
udp.bind()
udp.send({
address: '192.168.193.2',
port: 8848,
message: 'hello, how are you'
})

.setTTL

This method is used via UDPSocket.setTTL(number ttl).
Feature Description: Sets the IP_TTL socket option, which determines the maximum number of hops allowed for an IP packet during transmission.
Parameter and Description: number ttl, ranging between 0 and 255.
Sample Code
const udp = wx.createUDPSocket()
udp.onListening(function () {
udp.setTTL(64)
})
udp.bind()


.write

This method is used via UDPSocket.write().
Note:
Even if "connect" is called, it is necessary to input the address and port parameters in this interface.
Feature Description: Usage is the same as the send method, but not different from send if connect is not called beforehand.
Sample Code
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?
You can also Contact Sales or Submit a Ticket for help.
Yes
No

Feedback

Contact Us

Contact our sales team or business advisors to help your business.

Technical Support

Open a ticket if you're looking for further assistance. Our Ticket is 7x24 available.

7x24 Phone Support
Hong Kong, China
+852 800 906 020 (Toll Free)
United States
+1 844 606 0804 (Toll Free)
United Kingdom
+44 808 196 4551 (Toll Free)
Canada
+1 888 605 7930 (Toll Free)
Australia
+61 1300 986 386 (Toll Free)
EdgeOne hotline
+852 300 80699
More local hotlines coming soon