tencent cloud

Tencent Cloud Super App as a Service

Release Notes and Announcements
Announcement: Tencent Cloud Mini Program Platform Renamed to Tencent Cloud Super App as a Service on January 2, 2025
Console Updates
Android SDK Updates
iOS SDK Updates
Flutter SDK Updates
IDE Updates
Base Library Updates
Product Introduction
Overview
Strengths
Use Cases
Purchase Guide
Billing Overview
Pay-As-You-Go Billing
Renewal Guide
Service Suspension Instructions
Getting Started
Plan Management
Overview
Console Account Management
Storage Configuration
Acceleration Configuration
Branding Configurations
Platform Features
Console Login
Users and Permission System
Mini Program Management
Mini Game Management
Superapp Management
Commercialization
Platform Management
User Management
Team Management
Operations Management
Security Center
Code Integration Guide
Getting Demo and SDK
Android
iOS
Flutter
Superapp Server
GUID Generation Rules
Mini Program Development Guide
Mini Program Introduction and Development Environment
Mini Program Code Composition
Guide
Framework
Components
API
Server Backend
JS SDK
Base Library
IDE Operation Instructions
Mini Game Development Guide
Guide
API
Server Backend
Practice Tutorial
Mini Program Login Practical Tutorial
Mini Program Subscription Message Practical Tutorial
Payment Practical Tutorial
Ad Integration Practical Tutorial
Mini Game Subscription Message Practical Tutorial
API Documentation
History
Introduction
API Category
Making API Requests
Operation Management APIs
User Management APIs
Team Management APIs
Sensitive API-Related APIs
Role Management APIs
Platform Management APIs
Other Console APIs
Mini Program or Mini Game APIs
Management-Sensitive APIs
Global Domain Management APIs
Superapp APIs
Data Types
Agreements
Service Level Agreement
Data Processing and Security Agreement
SDK Privacy Policy Module
SDK Data Processing and Security Agreement Module

UDP Communication

PDF
Focus Mode
Font Size
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'
})


Help and Support

Was this page helpful?

Help us improve! Rate your documentation experience in 5 mins.

Feedback