Skip to content

gee1k/capacitor-udp-socket

Repository files navigation

capacitor-udp-socket

A UDP Socket Plugin for capacitor

Thanks @unitree-czk

Install

npm install capacitor-udp-socket
npx cap sync

API

UDP socket plugin for Capacitor.

Only available on Android and iOS.

create(...)

create(options?: CreateOptions | undefined) => Promise<CreateResult>

Create a socket for udp, and you can create more than one differentiated by the socket id.

Only available on Android and iOS.

Param Type
options CreateOptions

Returns: Promise<CreateResult>

Since: 5.0.0


update(...)

update(options: UpdateOptions) => Promise<void>

Update the socket info including socket name and buffer size.

Only available on Android and iOS.

Param Type
options UpdateOptions

Since: 5.0.0


bind(...)

bind(options: BindOptions) => Promise<void>

You need to bind a socket before sending and receiving data.

Only available on Android and iOS.

Param Type
options BindOptions

Since: 5.0.0


send(...)

send(options: SendOptions) => Promise<SendResult>

Send udp data

Only available on Android and iOS.

Param Type
options SendOptions

Returns: Promise<SendResult>

Since: 5.0.0


close(...)

close(options: CloseOptions) => Promise<void>

Close one socket

Only available on Android and iOS.

Param Type
options CloseOptions

Since: 5.0.0


closeAllSockets()

closeAllSockets() => Promise<void>

Close All Sockets

Only available on Android and iOS.

Since: 5.0.0


setBroadcast(...)

setBroadcast(options: SetBroadcastOptions) => Promise<void>

After enabling broadcasting, you can send data with target address 255.255.255.255.

Only available on Android and iOS.

Param Type
options SetBroadcastOptions

Since: 5.0.0


setPaused(...)

setPaused(options: SetPausedOptions) => Promise<void>

Pause receiving data.

Only available on Android and iOS.

Param Type
options SetPausedOptions

Since: 5.0.0


getInfo(...)

getInfo(options: InfoOptions) => Promise<InfoResult>

Get Socket information

Only available on Android and iOS.

Param Type
options InfoOptions

Returns: Promise<InfoResult>

Since: 5.0.0


getSockets()

getSockets() => Promise<GetSocketsResult>

Obtain all the sockets available.

Only available on Android and iOS.

Returns: Promise<GetSocketsResult>

Since: 5.0.0


joinGroup(...)

joinGroup(options: JoinGroupOptions) => Promise<void>

Join a particular group address. For IPv4, it's like "238.12.12.12". For IPv6, it's like "ff02::08".

Only available on Android and iOS.

Param Type
options JoinGroupOptions

Since: 5.0.0


leaveGroup(...)

leaveGroup(options: LeaveGroupOptions) => Promise<void>

Leave a particular group address. For IPv4, it's like "238.12.12.12". For IPv6, it's like "ff02::08".

Only available on Android and iOS.

Param Type
options LeaveGroupOptions

Since: 5.0.0


getJoinedGroups()

getJoinedGroups() => Promise<GetJoinedGroupsResult>

Get joined groups

Only available on Android and iOS.

Returns: Promise<GetJoinedGroupsResult>

Since: 5.0.0


setMulticastTimeToLive(...)

setMulticastTimeToLive(options: SetMulticastTimeToLiveOptions) => Promise<void>

Set the time to live (TTL) for multicast packets

Only available on Android and iOS.

Param Type
options SetMulticastTimeToLiveOptions

Since: 5.0.0


setMulticastLoopbackMode(...)

setMulticastLoopbackMode(options: SetMulticastLoopbackModeOptions) => Promise<void>

Set whether to enable multicast loopback mode

Only available on Android and iOS.

Param Type
options SetMulticastLoopbackModeOptions

Since: 5.0.0


addListener('receive', ...)

addListener(eventName: 'receive', listenerFunc: (event: ReceiveEvent) => void) => Promise<PluginListenerHandle>

Listening for data reception events

Only available on Android and iOS.

Param Type
eventName 'receive'
listenerFunc (event: ReceiveEvent) => void

Returns: Promise<PluginListenerHandle>

Since: 5.0.0


addListener('receiveError', ...)

addListener(eventName: 'receiveError', listenerFunc: (event: ReceiveEvent) => void) => Promise<PluginListenerHandle>

Listening for data reception exception events

Only available on Android and iOS.

Param Type
eventName 'receiveError'
listenerFunc (event: ReceiveEvent) => void

Returns: Promise<PluginListenerHandle>

Since: 5.0.0


Interfaces

CreateResult

Result of creating a UDP socket

Prop Type Description
socketId number The id of the socket
ipv4 string ipv4 address
ipv6 string ipv6 address

CreateOptions

Parameters for creating a UDP socket

Prop Type
properties { name?: string; bufferSize?: number; }

UpdateOptions

Parameters for updating a UDP socket

Prop Type Description
socketId number The id of the socket
properties { name?: string; bufferSize?: number; }

BindOptions

Parameters for binding a UDP socket

Prop Type Description
socketId number The id of the socket
address string The address to bind to. If not specified, the socket will be bound to the wildcard address.
port number The port to bind to.

SendResult

Result of sending data

Prop Type
bytesSent number

SendOptions

Parameters for sending data

Prop Type Description
socketId number The id of the socket
address string The address of the remote host.
port number The port of the remote host.
buffer string The data to send.

CloseOptions

Parameters for closing a UDP socket

Prop Type Description
socketId number The id of the socket

SetBroadcastOptions

Parameters for settings broadcast mode

Prop Type Description
socketId number The id of the socket
enabled boolean Whether to enable broadcast mode.

SetPausedOptions

Parameters for binding a UDP socket

Prop Type Description
socketId number The id of the socket
paused boolean Whether to pause receiving data

InfoResult

Result of getting information about a UDP socket

Prop Type Description
socketId number The id of the socket
name string | null The name of the socket, which can be used to distinguish between multiple sockets.
bufferSize number The size of the buffer used to receive data.
paused boolean Whether data reception has been suspended。
localAddress string The address to which the socket is bound.
localPort number The port to which the socket is bound.

InfoOptions

Parameters for getting information about a UDP socket

Prop Type Description
socketId number The id of the socket

GetSocketsResult

Parameters for getting information about all UDP sockets

Prop Type Description
sockets InfoResult[] The list of UDP sockets

JoinGroupOptions

Parameters for joining a multicast group

Prop Type Description
socketId number The id of the socket
address string The address of the multicast group to join. For IPv4, it's like "238.12.12.12". For IPv6, it's like "ff02::08".

LeaveGroupOptions

Parameters for leaving a multicast group

Prop Type Description
socketId number The id of the socket
address string The address of the multicast group to leave. For IPv4, it's like "238.12.12.12". For IPv6, it's like "ff02::08".

GetJoinedGroupsResult

Parameters for getting joined multicast groups

Prop Type Description
groups [string] The list of multicast group addresses

SetMulticastTimeToLiveOptions

Parameters for setting multicast time to live

Prop Type Description
socketId number The id of the socket
ttl number The time to live value.

SetMulticastLoopbackModeOptions

Parameters for setting multicast loopback mode

Prop Type Description
socketId number The id of the socket
enabled boolean Whether to enable multicast loopback mode.

PluginListenerHandle

Prop Type
remove () => Promise<void>

ReceiveEvent

Result of receiving data

Prop Type Description
socketId number The id of the socket
buffer string The data received.
remoteAddress string The address of the remote host.
remotePort number The port of the remote host.
error string Error message