Skip to main content
Version: 3.x

UDPSocket

An UDP Socket instance. Use IPv4 protocol by default.

Reference

Methods

close

Closes (Terminates) a UDP Socket instance. After closing, the UDP Socket instance can no longer send messages. Calling UDPSocket.send will trigger an error event, and the callback function for the message event will not be executed. After the UDPSocket instance is created, it will be strongly referenced by Native to ensure that it is not collected by GC. After calling UDPSocket.close, the strong reference to it will be removed, and the UDPSocket instance will follow the GC rules.

Reference

() => void

API Support

APIWeChat Mini-ProgramH5React Native
UDPSocket.close✔️

offClose

Un-listens on the disabling event.

Reference

(callback: OffCloseCallback) => void
PropertyTypeDescription
callbackOffCloseCallbackThe callback function for the disabling event.

API Support

APIWeChat Mini-ProgramH5React Native
UDPSocket.offClose✔️

offError

Un-listens on the error event.

Reference

(callback: OffErrorCallback) => void
PropertyTypeDescription
callbackOffErrorCallbackThe callback function for the error event.

API Support

APIWeChat Mini-ProgramH5React Native
UDPSocket.offError✔️

offListening

Un-listens on the event of starting listening on data packet messages.

Reference

(callback: (res: CallbackResult) => void) => void
PropertyTypeDescription
callback(res: CallbackResult) => voidThe callback function for the event of starting listening on data packet messages.

API Support

APIWeChat Mini-ProgramH5React Native
UDPSocket.offListening✔️

offMessage

Un-listens on the event of receiving messages.

Reference

(callback: (res: CallbackResult) => void) => void
PropertyTypeDescription
callback(res: CallbackResult) => voidThe callback function for the event of receiving messages.

API Support

APIWeChat Mini-ProgramH5React Native
UDPSocket.offMessage✔️

onClose

Listens on the disabling event.

Reference

(callback: OnCloseCallback) => void
PropertyTypeDescription
callbackOnCloseCallbackThe callback function for the disabling event.

API Support

APIWeChat Mini-ProgramH5React Native
UDPSocket.onClose✔️

onError

Listens on the error event.

Reference

(callback: OnErrorCallback) => void
PropertyTypeDescription
callbackOnErrorCallbackThe callback function for the error event.

API Support

APIWeChat Mini-ProgramH5React Native
UDPSocket.onError✔️

onListening

Listens on the event of starting listening on data packet messages.

Reference

(callback: (res: CallbackResult) => void) => void
PropertyTypeDescription
callback(res: CallbackResult) => voidThe callback function for the event of starting listening on data packet messages.

API Support

APIWeChat Mini-ProgramH5React Native
UDPSocket.onListening✔️

onMessage

Listens on the event of receiving messages.

Reference

(callback: OnMessageCallback) => void
PropertyTypeDescription
callbackOnMessageCallbackThe callback function for the event of receiving messages.

API Support

APIWeChat Mini-ProgramH5React Native
UDPSocket.onMessage✔️

send

Sends messages to the specified IP and port.

Reference

(option: SendOption) => void
PropertyType
optionSendOption

API Support

APIWeChat Mini-ProgramH5React Native
UDPSocket.send✔️

bind

Binds an available port randomly assigned by the system.

Reference

(port: number) => number
PropertyTypeDescription
portnumberBound port number

API Support

APIWeChat Mini-ProgramH5React Native
UDPSocket.bind✔️

Parameters

OffCloseCallback

The callback function for the disabling event.

(res: CallbackResult) => void
PropertyType
resCallbackResult

OffErrorCallback

The callback function for the error event.

(res: CallbackResult) => void
PropertyType
resCallbackResult

OnCloseCallback

The callback function for the disabling event.

(res: CallbackResult) => void
PropertyType
resCallbackResult

OnErrorCallback

The callback function for the error event.

(result: OnErrorCallbackResult) => void
PropertyType
resultOnErrorCallbackResult

OnMessageCallback

The callback function for the event of receiving messages.

(result: OnMessageCallbackResult) => void
PropertyType
resultOnMessageCallbackResult

OnErrorCallbackResult

PropertyTypeDescription
errMsgstring错误信息

OnMessageCallbackResult

PropertyTypeDescription
messageArrayBufferReceived messages
remoteInfoRemoteInfoStructured information of message sources

RemoteInfo

remoteInfo is composed as follows

PropertyTypeDescription
addressstringThe address of the socket sending the message
familystringThe protocol family used, IPv4 or IPv6
portnumberPort number
sizenumberThe message size, in bytes

SendOption

PropertyTypeRequiredDescription
datastring | ArrayBufferYesThe data to be sent
complete(res: CallbackResult) => voidNoThe callback function used when the API call completed (always executed whether the call succeeds or fails)
fail(res: CallbackResult) => voidNoThe callback function for a failed API call
success(res: CallbackResult) => voidNoThe callback function for a successful API call

API Support

APIWeChat Mini-ProgramH5React Native
UDPSocket.close✔️
UDPSocket.offClose✔️
UDPSocket.offError✔️
UDPSocket.offListening✔️
UDPSocket.offMessage✔️
UDPSocket.onClose✔️
UDPSocket.onError✔️
UDPSocket.onListening✔️
UDPSocket.onMessage✔️
UDPSocket.send✔️
UDPSocket.bind✔️