Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

WeChat small program API UDP communication


May 18, 2021 WeChat Mini Program Development Document


Table of contents


UDPSocket wx.createUDPSocket()

Base library 2.7.0 starts to support, and low versions need to be compatible.

Create a UDP Socket instance. Please read the instructions before use.

Returns a value

UDPSocket

A UDP Socket instance


UDPSocket

Base library 2.7.0 starts to support, and low versions need to be compatible.

A UDP Socket instance that uses the IPv4 protocol by default.

Method:

number UDPSocket.bind(number port)

Bind an available port randomly assigned by the system, or bind a specified port number

Parameters

number port

Base library 2.9.0 starts to support, and low versions need to be compatible.

Specify the port number to bind, and return the available ports randomly assigned by the system without passing

Returns a value

number

The port number that was successfully bound

The sample code

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


UDPSocket.close()

Closing a UDP Socket instance is equivalent to destroying it. A fter closing, the UDP Socket instance can no longer send messages, and each call to UDPSocket.send triggers an error event, and the message event callback function is no longer executed. W hen a UDPSocket instance is created, it is strongly referenced by Native to ensure that it is not GC. After UDPSocket.close, the strong reference to it is lifted and the UDPSocket instance follows the GC.


UDPSocket.offClose(function callback)

Cancel the listening shutdown event

Parameters

function callback

Close the callback function for the event


UDPSocket.offError(function callback)

Cancel listening for error events

Parameters

function callback

The callback function for the error event


UDPSocket.offListening(function callback)

Cancel the event that began listening for packet messages

Parameters

function callback

A callback function that starts listening for events for packet messages


UDPSocket.offMessage(function callback)

Cancel listening for events that receive messages

Parameters

function callback

The callback function for the event that received the message


UDPSocket.onClose(function callback)

Listen for shutdown events

Parameters

function callback

Close the callback function for the event


UDPSocket.onError(function callback)

Listen for error events

Parameters

function callback

The callback function for the error event

Parameters

Object res
Property Type Description
errMsg string The error message


UDPSocket.onListening(function callback)

Listen for events that start listening for packet messages

Parameters

function callback

A callback function that starts listening for events for packet messages


UDPSocket.onMessage(function callback)

Listen for events that receive messages

Parameters

function callback

The callback function for the event that received the message

Parameters

Object res
Property Type Description
message ArrayBuffer The message received
remoteInfo Object Structured information about the source

The structure of remoteInfo

Property Type Description
address string The address of the socket that sent the message
family string The protocol family used is IPv4 or IPv6
port number The port number
size number The size of the message in bytes


UDPSocket.send(Object object)

Send messages to the specified IP and port

Parameters

Object object

Property Type The default Required Description
address string Is The address to send the message. The underlying library 2.9.3 and previous versions can be an IP address with the same segment as the local domain, or a domain name address within the list of secure domain names, and any IP and domain name in the base library 2.9.4 and later
port number Is The port number to send the message
message string/ArrayBuffer Is The data to send
offset number 0 Whether The offset of the data sent is only valid if the message is of the ArrayBuffer type
length number message.byteLength Whether The length of the data sent is valid only if the message is of the ArrayBuffer type

The sample code


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