tencent cloud

Feedback

UDP Communication

Last updated: 2024-03-04 23:11:57

    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

    .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, 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) }
    
    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: Object res parameter, function listener, the listener function for events of receiving a message.
    Attribute
    Type
    Note
    message
    ArrayBuffer
    The received message. The length of the message must be less than 4096.
    remoteInfo
    Object
    Sender's address information
    localInfo
    Object
    Recipient's address information, supported from V2.18.0 onwards.
    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 receiving the message
    family
    string
    The protocol family in use, either IPv4 or IPv6.
    Port No.
    number
    Port No.

    .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 which the message is to be sent. In the base library version 2.9.3 and earlier, it must be an IP address in the same network segment as the local device, or a domain name address within the security domain list; in the base library version 2.9.3 and later, it can be any IP and domain name.
    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().
    Feature Description: The usage is identical to the "send" method, and if "connect" is not called in advance, there is no difference from "send".
    Note:
    Even if "connect" is called, it is necessary to input the address and port parameters in this interface.
    
    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