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

TCP Communication

PDF
Focus Mode
Font Size
Last updated: 2025-03-20 18:01:56

createTCPSocket

This API is used via TCPSocket wx.createTCPSocket().
Feature description: Creates a TCP Socket instance.
Return Value: A TCP Socket instance.
Link limitations
Allows communication with non-local IP within the local area network.
Allows communication with configured server domain names.
Prohibits connections to the following port numbers: below 1024, 1099, 1433, 1521, 1719, 1720, 1723, 2049, 2375, 3128, 3306, 3389, 3659, 4045, 5060, 5061, 5432, 5984, 6379, 6000, 6566, 7001, 7002, 8000-8100, 8443, 8888, 9200, 9300, 10051, 10080, 11211, 27017, 27018, 27019.
A maximum of 20 TCPSockets can be created within every 5-minute interval.

TCPSocket

Note:
A TCP Socket instance, using IPv4 protocol by default.
When errCode is -2, the corresponding errno should be present in errMsg. Developers can examine the specific error message in the Linux code's errno-base.h and errno.h based on the errno.

.bindWifi

This method is used via TCPSocket.bindWifi(Object options).
Feature Description: Binds the TCP Socket to the current Wi-Fi network, which will, upon successful execution, trigger the onBindWifi event (supported only on Android).
Parameter and Description: Object options.
Attribute
Type
Default value
Required
Note
BSSID
string
-
Yes
The BSSID of the current Wi-Fi network, which can be obtained through wx.getConnectedWifi.
Sample Code
const tcp = wx.createTCPSocket()
tcp.bindWifi({ BSSID: 'xxx' })
tcp.onBindWifi(() => {})

.close

This method is used via TCPSocket.close().
Feature Description: Disables connection.
Sample Code
const tcp = wx.createTCPSocket()
tcp.close()

.connect

This method is used via TCPSocket.connect(Object options).
Feature Description: Initiates a connection on the specified socket.
Parameter and Description: Object options.
Attribute
Type
Default value
Required
Note
address
string
-
Yes
The address to which the socket is to be connected.
Port No.
number
-
Yes
The port to which the socket is to be connected.
timeout
number
2
No
The timeout duration for the socket connection, defaulting to 2 seconds.
Sample Code
const tcp = wx.createTCPSocket()
tcp.connect({address: '192.168.193.2', port: 8848})

.onClose

This method is used via TCPSocket.onClose(function listener).
Feature Description: Monitors events that are emitted once the socket is fully closed.
Parameter and Description: function listener, the listener function for events triggered once the socket is fully closed.

.offClose

This method is used via TCPSocket.offClose(function listener).
Feature Description: Removes the listener function for events that are emitted once the socket is fully closed.
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) }

TCPSocket.onConnect(listener)
TCPSocket.offConnect(listener) // The same function object as the listener must be passed in.

.onConnect

This method is used via TCPSocket.onConnect(function listener).
Feature Description: Monitors events that are triggered when a socket connection is successfully established.
Parameter and Description: function listener, the listener function for events triggered when a socket connection is successfully established.

.offConnect

This method is used via TCPSocket.offConnect(function listener).
Feature Description: Removes the listener function for events triggered when a socket connection is successfully established.
Parameter and Description: function listener, the listener function passed in by onConnect. If this parameter is not passed in, all listener functions will be removed.
Sample Code
const listener = function (res) { console.log(res) }

TCPSocket.onConnect(listener)
TCPSocket.offConnect(listener) // The same function object as the listener must be passed in.

.onError

This method is used via TCPSocket.onError(function listener).
Feature Description: Monitoring is triggered when an error occurs.
Parameters and Description: The listener function of function listener.
Attribute
Type
Note
errMsg
string
Error Message

.offError

This method is used via TCPSocket.offError(function listener).
Feature Description: Removes the listener function that is triggered when an error occurs.
Parameter and Description: function listener, the listener function passed in by onError. If this parameter is not passed in, all listener functions will be removed.
Sample Code
const listener = function (res) { console.log(res) }

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

.onMessage

This method is used via TCPSocket.onMessage(function listener).
Feature Description: Monitors events that are triggered when data is received.
Parameter and Description: function listener, when receiving data to trigger the event of the listener function, the parameter Object res as follows:
Attribute
Type
Note
message
ArrayBufffer
Received message
remoteInfo
Object
Sender's address information
localInfo
Object
Recipient'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.
Structure attributes of "localInfo"
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.

.offMessage

This method is used via TCPSocket.offMessage(function listener).
Feature Description: Removes the listener function of the event that is triggered when data is received.
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) }

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

.onBindWifi

This method is used via TCPSocket.onBindWifi(function listener).
Feature Description: Monitors events triggered when a socket successfully binds to the current Wi-Fi network.
Parameter and Description: function listener, the listener function for events triggered when a socket successfully binds to the current Wi-Fi network.

.offBindWifi

This method is used via TCPSocket.offBindWifi(function listener).
Feature Description: Removes the listener function for events triggered when a socket successfully binds to the current Wi-Fi network.
Parameters and Description: function listener, the listener function passed in by onBindWifi. If this parameter is not passed in, all listener functions will be removed.
Sample Code
const listener = function (res) { console.log(res) }

TCPSocket.onBindWifi(listener)
TCPSocket.offBindWifi(listener) // The same function object as the listener must be passed in.

.write

This method is used via TCPSocket.write(string|ArrayBuffer data).
Feature Description: Sends data over the socket.
Parameter and Description: string|ArrayBuffer data, the data to be sent.
Sample Code
const tcp = wx.createTCPSocket()
tcp.write('hello, how are you')


Help and Support

Was this page helpful?

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

Feedback