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.
|
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.
|
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)
.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)
.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.
|
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)
.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:
|
message | ArrayBufffer | Received message |
remoteInfo | Object | Sender's address information |
localInfo | Object | Recipient's address information |
Structure attributes of "remoteInfo"
|
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"
|
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)
.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)
.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')
Was this page helpful?