title: UDPSocket

sidebar_label: 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

  1. () => void

API Support

API WeChat Mini-Program H5 React Native
UDPSocket.close ✔️

offClose

Un-listens on the disabling event.

Reference

  1. (callback: OffCloseCallback) => void
Property Type Description
callback OffCloseCallback The callback function for the disabling event.

API Support

API WeChat Mini-Program H5 React Native
UDPSocket.offClose ✔️

offError

Un-listens on the error event.

Reference

  1. (callback: OffErrorCallback) => void
Property Type Description
callback OffErrorCallback The callback function for the error event.

API Support

API WeChat Mini-Program H5 React Native
UDPSocket.offError ✔️

offListening

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

Reference

  1. (callback: (res: CallbackResult) => void) => void
Property Type Description
callback (res: CallbackResult) => void The callback function for the event of starting listening on data packet messages.

API Support

API WeChat Mini-Program H5 React Native
UDPSocket.offListening ✔️

offMessage

Un-listens on the event of receiving messages.

Reference

  1. (callback: (res: CallbackResult) => void) => void
Property Type Description
callback (res: CallbackResult) => void The callback function for the event of receiving messages.

API Support

API WeChat Mini-Program H5 React Native
UDPSocket.offMessage ✔️

onClose

Listens on the disabling event.

Reference

  1. (callback: OnCloseCallback) => void
Property Type Description
callback OnCloseCallback The callback function for the disabling event.

API Support

API WeChat Mini-Program H5 React Native
UDPSocket.onClose ✔️

onError

Listens on the error event.

Reference

  1. (callback: OnErrorCallback) => void
Property Type Description
callback OnErrorCallback The callback function for the error event.

API Support

API WeChat Mini-Program H5 React Native
UDPSocket.onError ✔️

onListening

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

Reference

  1. (callback: (res: CallbackResult) => void) => void
Property Type Description
callback (res: CallbackResult) => void The callback function for the event of starting listening on data packet messages.

API Support

API WeChat Mini-Program H5 React Native
UDPSocket.onListening ✔️

onMessage

Listens on the event of receiving messages.

Reference

  1. (callback: OnMessageCallback) => void
Property Type Description
callback OnMessageCallback The callback function for the event of receiving messages.

API Support

API WeChat Mini-Program H5 React Native
UDPSocket.onMessage ✔️

send

Sends messages to the specified IP and port.

Reference

  1. (option: SendOption) => void
Property Type
option SendOption

API Support

API WeChat Mini-Program H5 React Native
UDPSocket.send ✔️

bind

Binds an available port randomly assigned by the system.

Reference

  1. (port: number) => number
Property Type Description
port number Bound port number

API Support

API WeChat Mini-Program H5 React Native
UDPSocket.bind ✔️

Parameters

OffCloseCallback

The callback function for the disabling event.

  1. (res: CallbackResult) => void
Property Type
res CallbackResult

OffErrorCallback

The callback function for the error event.

  1. (res: CallbackResult) => void
Property Type
res CallbackResult

OnCloseCallback

The callback function for the disabling event.

  1. (res: CallbackResult) => void
Property Type
res CallbackResult

OnErrorCallback

The callback function for the error event.

  1. (result: OnErrorCallbackResult) => void
Property Type
result OnErrorCallbackResult

OnMessageCallback

The callback function for the event of receiving messages.

  1. (result: OnMessageCallbackResult) => void
Property Type
result OnMessageCallbackResult

OnErrorCallbackResult

Property Type Description
errMsg string 错误信息

OnMessageCallbackResult

Property Type Description
message ArrayBuffer Received messages
remoteInfo RemoteInfo Structured information of message sources

RemoteInfo

remoteInfo is composed as follows

Property Type Description
address string The address of the socket sending the message
family string The protocol family used, IPv4 or IPv6
port number Port number
size number The message size, in bytes

SendOption

Property Type Required Description
data string | ArrayBuffer Yes The data to be sent
complete (res: CallbackResult) => void No The callback function used when the API call completed (always executed whether the call succeeds or fails)
fail (res: CallbackResult) => void No The callback function for a failed API call
success (res: CallbackResult) => void No The callback function for a successful API call

API Support

API WeChat Mini-Program H5 React Native
UDPSocket.close ✔️
UDPSocket.offClose ✔️
UDPSocket.offError ✔️
UDPSocket.offListening ✔️
UDPSocket.offMessage ✔️
UDPSocket.onClose ✔️
UDPSocket.onError ✔️
UDPSocket.onListening ✔️
UDPSocket.onMessage ✔️
UDPSocket.send ✔️
UDPSocket.bind ✔️