tencent cloud

Feedback

TCP Communication

Last updated: 2024-03-05 15:02:11

    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: Object res parameter, function listener, the listener function of the events triggered when data is received.
    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')
    
    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 avaliable.

    7x24 Phone Support