Skip to content

Tcp Communication

createTCPSocket

TIP

The API usage is as follows: TCPSocket wx.createTCPSocket()

  • Functional description: Create a TCP Socket instance.
  • Return value: A TCP Socket instance.
  • 链接限制
    • Allow communication with non-local IP in LAN;
    • Allow communication with configured server domain name, see Related instructions.
    • Prohibit connection with 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 every 5 minutes.

TCPSocket

Description

  • A TCP Socket instance, using IPv4 protocol by default.
  • When errCode is -2, there should be a corresponding errno in errMsg. Developers can use errno to find the error in the Linux code. errno-base.h and errno.h

.bindWifi

TIP

The API usage is as follows: TCPSocket.bindWifi(Object options)

  • Functional description: Bind the TCP Socket to the current Wi-Fi network. If successful, it will trigger the onBindWifi event (only supported by Android)

  • Parameters and descriptions: Object options。

    PropertiesTypeDefault valueRequiredDescription
    BSSIDstring-YesThe BSSID of the current Wi-Fi network can be obtained through wx.getConnectedWifi
  • Sample code:

js
const tcp = wx.createTCPSocket()  
tcp.bindWifi({ BSSID: 'xxx' })  
tcp.onBindWifi(() => {})

.close

TIP

The API usage is as follows: TCPSocket.close()

  • Functional description: Close the connection.

  • Sample code:

js
const tcp = wx.createTCPSocket()  
tcp.close()

.connect

TIP

The API usage is as follows: TCPSocket.connect(Object options)

  • Functional description: Start a connection on the given socket.

  • Parameters and descriptions: Object options。

    PropertiesTypeDefault valueRequiredDescription
    addressstring-YesThe address the socket is connecting to
    portnumber-YesThe port the socket is connecting to
    timeoutnumber2NoThe timeout for the socket to connect, defaults to 2s
  • Sample code:

js
const tcp = wx.createTCPSocket()  
tcp.connect({address: '192.168.193.2', port: 8848})

.onClose

TIP

The API usage is as follows: TCPSocket.onClose(function listener)

  • Functional description: Listener to emit this event once the socket is fully closed.
  • Parameters and descriptions: function listener,Listener function to emit this event once the socket is fully closed.。

.offClose

TIP

The API usage is as follows: TCPSocket.offClose(function listener)

  • Functional description: Remove listener function to emit this event once the socket is fully closed.

  • Parameters and descriptions: function listener onClose The passed listener function. Without this parameter, all listeners are removed.

  • Sample code:

js
const listener = function (res) { console.log(res) }

TCPSocket.onConnect(listener)
TCPSocket.offConnect(listener)  // Pass the same function object used for listening.

.onConnect

TIP

The API usage is as follows: TCPSocket.onConnect(function listener)

  • Functional description: Listener to trigger this event when a socket connection is successfully established.
  • Parameters and descriptions: function listener,Listener function to trigger this event when a socket connection is successfully established.。

.offConnect

TIP

The API usage is as follows: TCPSocket.offConnect(function listener)

  • Functional description: Remove listener function to trigger this event when a socket connection is successfully established.

  • Parameters and descriptions: function listener onConnect The passed listener function. Without this parameter, all listeners are removed.

  • Sample code:

js
const listener = function (res) { console.log(res) }

TCPSocket.onConnect(listener)
TCPSocket.offConnect(listener) // Pass the same function object used for listening.

.onError

TIP

The API usage is as follows: TCPSocket.offError(function listener)

  • Functional description: Listener to trigger when an error occurs.

  • Parameters and descriptions: Listener function of function listener.

    PropertiesTypeDescription
    errMsgstringError message

.offError

TIP

The API usage is as follows: TCPSocket.offError(function listener)

  • Functional description: Remove listener function to trigger when an error occurs.

  • Parameters and descriptions: function listener, onError The passed listener function. Without this parameter, all listeners are removed.

  • Sample code:

js
const listener = function (res) { console.log(res) }

TCPSocket.onError(listener)
TCPSocket.offError(listener)  // Pass the same function object used for listening.

.onMessage

TIP

The API usage is as follows: TCPSocket.onMessage(function listener)

  • Functional description: Listen for this event when data is received

  • Parameters and descriptions: function listener, When data is received, the listener function of this event is triggered. The parameter Object res is as follows:

    PropertiesTypeDescription
    messageArrayBufferReceived message
    remoteInfoObjectSender address information
    localInfoObjectReceiver address information
    • remoteInfo structure attribute

      PropertiesTypeDescription
      addressstringThe address of the socket to send the message
      familystringThe protocol family used, which is IPv4 or IPv7
      portnumberPort number
      sizenumberThe size of the message, in bytes
    • localInfo structure attributes

      PropertiesTypeDescription
      addressstringThe address of the socket to send the message
      familystringThe protocol family used, which is IPv4 or IPv7
      portnumberPort number

.offMessage

TIP

The API usage is as follows: TCPSocket.offMessage(function listener)

  • Functional description: Removes the listener function that triggers this event when data is received.

  • Parameters and descriptions: function listener, onMessage The listener function passed in. Removes all listeners if this parameter is not passed.

  • Sample code:

js
const listener = function (res) { console.log(res) }

TCPSocket.onMessage(listener)
TCPSocket.offMessage(listener) // Pass the same function object used for listening.

.onBindWifi

TIP

The API usage is as follows: TCPSocket.onBindWifi(function listener)

  • Functional description: Listens for the event that is triggered when a socket is successfully bound to the current Wi-Fi network.
  • Parameters and descriptions: function listener, The listener function that is triggered when a socket is successfully bound to the current Wi-Fi network.

.offBindWifi

TIP

The API usage is as follows: TCPSocket.offBindWifi(function listener)

  • Functional description: Removes the listener function that is triggered when a socket is successfully bound to the current Wi-Fi network.

  • Parameters and descriptions: function listener, onBindWifi The listener function passed in. Removes all listeners if this parameter is not passed.

  • Sample code:

js
const listener = function (res) { console.log(res) }

TCPSocket.onBindWifi(listener)
TCPSocket.offBindWifi(listener) // Pass the same function object used for listening.

.write

TIP

The API usage is as follows: TCPSocket.write(string|ArrayBuffer data)

  • Functional description: Sends data on the socket.

  • Parameters and descriptions: string|ArrayBuffer data, The data to be sent.

  • Sample code:

js
const tcp = wx.createTCPSocket()  
tcp.write('hello, how are you')