Skip to content

Udp Communication

createUDPSocket

TIP

The API usage is as follows: UDPSocket wx.createUDPSocket()

  • Functional description: Create a UDP Socket instance.
  • Return value: UDPSocket, a UDP Socket instance.

UDPSocket

Description

A UDP Socket instance uses IPv4 protocol by default.

.bind

TIP

The API usage is as follows: number UDPSocket.bind(number port)

  • Functional description: Bind an available port randomly assigned by the system, or bind a specified port number.

  • Parameters and descriptions: number port, Specify the port number to bind, if not passed, return the available port randomly assigned by the system.

  • Return value: number, The port number of successful binding.

  • Sample code:

js
const udp = wx.createUDPSocket()
const port = udp.bind()

.close

TIP

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

  • Functional description: Close the UDP Socket instance, which is equivalent to destruction. After closing, 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 the UDPSocket instance is created, it will be strongly referenced by Native to ensure that it is not GC. After UDPSocket.close, the strong reference to it will be released to make the UDPSocket instance comply with GC.

.connect

TIP

The API usage is as follows: UDPSocket.connect(Object object)

  • Functional description: Pre-connect to the specified IP and port, need to be used with the write method.

  • Parameters and descriptions: Object object

    PropertiesTypeDefault valueRequiredDescription
    addressstring-YesAddress to send the message to
    portnumber-YesPort number to send the message to
  • Sample code:

js
  const udp = wx.createUDPSocket()
  udp.bind()
  udp.connect({
    address: '192.168.193.2',
    port: 8848,
  })
  udp.write({
    address: '192.168.193.2',
    port: 8848,
    message: 'hello, how are you'
  })

.onClose

TIP

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

  • Functional description: Listen for closing events.
  • Parameters and descriptions: function listener Listening function for close event.

.offClose

TIP

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

  • Functional description: Remove the listener function for closing events.

  • Parameters and descriptions: function listener Listening function passed in by onClose, if this parameter is not passed, all listening functions will be removed.

  • Sample code:

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

UDPSocket.onClose(listener)
UDPSocket.offClose(listener) // Pass the same function object used for listening.

.onError

TIP

The API usage is as follows: UDPSocket.onError(function listener)

  • Functional description: Listen for error events.

  • Parameters and descriptions: function listener, Listening function for error events, parameter Object res is as follows

    PropertiesTypeDescription
    errMsgstringError message

.offError

TIP

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

  • Functional description: Remove the listener function for error events.

  • Parameters and descriptions: function listener, onError The listening function passed in. If this parameter is not passed, all listening functions will be removed.

  • Sample code:

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

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

.onListening

TIP

The API usage is as follows: UDPSocket.onListening(function listener)

  • Functional description: Listen for events that start listening for data packet messages.
  • Parameters and descriptions: function listener, The listening function of the event of starting to listen for data packet messages.

.offListening

TIP

The API usage is as follows: UDPSocket.offListening(function listener)

  • Functional description: Remove the listener function for events that start listening for data packet messages.

  • Parameters and descriptions: function listener, onListening The listening function passed in. If this parameter is not passed, all listening functions will be removed.

  • Sample code:

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

UDPSocket.onListening(listener)
UDPSocket.offListening(listener) // Pass the same function object used for listening.

.onMessage

TIP

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

  • Functional description: Listen for the event of receiving messages.

  • Parameters and descriptions: function listener, the listening function of the event of receiving messages, the parameter Object res is as follows:

    PropertiesTypeDescription
    messageArrayBufferReceived message. The message length needs to be less than 4101
    remoteInfoObjectSender address information
    • remoteInfo structure attribute
      PropertiesTypeDescription
      addressstringThe address of the socket to send the message
      familystringThe protocol family used, which is IPv4 or IPv11
      portnumberPort number
      sizenumberThe size of the message, in bytes

.offMessage

TIP

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

  • Functional description: Remove the listener function of the event that receives the message.

  • Parameters and descriptions: function listener, onMessage The listening function passed in. If this parameter is not passed, all listening functions will be removed.

  • Sample code:

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

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

.send

TIP

The API usage is as follows: UDPSocket.send(Object object)

  • Functional description: Send a message to the specified IP and port.

  • Parameters and descriptions: Object object。

    PropertiesTypeDefault valueRequiredDescription
    addressstring-YesThe address to send the message.
    portnumber-YesThe port number to send the message to
    messagestring/ArrayBuffer-YesThe data to send
    offsetnumber0NoThe offset of the data to send, valid only when message is of ArrayBuffer type
    lengthnumbermessage.byteLengthNoThe length of the data to send, valid only when message is of ArrayBuffer type
  • Sample code:

js
const udp = wx.createUDPSocket()
  udp.bind()
  udp.send({
    address: '192.168.193.2',
    port: 8848,
    message: 'hello, how are you'
 })

.setTTL

TIP

The API usage is as follows: UDPSocket.setTTL(number ttl)

  • Functional description: Set the IP_TTL socket option to set the maximum number of hops allowed when transmitting an IP data packet.

  • Parameters and descriptions: number ttl, The ttl parameter can be between 0 and 255.

  • Sample code:

js
const udp = wx.createUDPSocket()
udp.onListening(function () {
  udp.setTTL(64)
})
udp.bind()

.write

TIP

The API usage is as follows: UDPSocket.write()

  • Functional description: The usage is the same as the send method. If connect is not called in advance, there is no difference with send.

Description

Even if connect is called, the address and port parameters need to be filled in this interface.

  • Sample code:
js
  const udp = wx.createUDPSocket()
  udp.bind()
  udp.connect({
    address: '192.168.193.2',
    port: 8848,
  })
  udp.write({
    address: '192.168.193.2',
    port: 8848,
    message: 'hello, how are you'
  })