A UDP Socket Plugin for capacitor
npm install capacitor-udp-socketA UDP Socket Plugin for capacitor
Thanks @unitree-czk
``bash`
npm install capacitor-udp-socket
npx cap sync
* create(...)
* update(...)
* bind(...)
* send(...)
* close(...)
* closeAllSockets()
* setBroadcast(...)
* setPaused(...)
* getInfo(...)
* getSockets()
* joinGroup(...)
* leaveGroup(...)
* getJoinedGroups()
* setMulticastTimeToLive(...)
* setMulticastLoopbackMode(...)
* addListener('receive', ...)
* addListener('receiveError', ...)
* Interfaces
UDP socket plugin for Capacitor.
Only available on Android and iOS.
`typescript`
create(options?: CreateOptions | undefined) => Promise
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
--------------------
`typescript`
update(options: UpdateOptions) => Promise
Update the socket info including socket name and buffer size.
Only available on Android and iOS.
| Param | Type |
| ------------- | ------------------------------------------------------- |
| options | UpdateOptions |
Since: 5.0.0
--------------------
`typescript`
bind(options: BindOptions) => Promise
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
--------------------
`typescript`
send(options: SendOptions) => Promise
Send udp data
Only available on Android and iOS.
| Param | Type |
| ------------- | --------------------------------------------------- |
| options | SendOptions |
Returns: Promise<SendResult>
Since: 5.0.0
--------------------
`typescript`
close(options: CloseOptions) => Promise
Close one socket
Only available on Android and iOS.
| Param | Type |
| ------------- | ----------------------------------------------------- |
| options | CloseOptions |
Since: 5.0.0
--------------------
`typescript`
closeAllSockets() => Promise
Close All Sockets
Only available on Android and iOS.
Since: 5.0.0
--------------------
`typescript`
setBroadcast(options: SetBroadcastOptions) => Promise
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
--------------------
`typescript`
setPaused(options: SetPausedOptions) => Promise
Pause receiving data.
Only available on Android and iOS.
| Param | Type |
| ------------- | ------------------------------------------------------------- |
| options | SetPausedOptions |
Since: 5.0.0
--------------------
`typescript`
getInfo(options: InfoOptions) => Promise
Get Socket information
Only available on Android and iOS.
| Param | Type |
| ------------- | --------------------------------------------------- |
| options | InfoOptions |
Returns: Promise<InfoResult>
Since: 5.0.0
--------------------
`typescript`
getSockets() => Promise
Obtain all the sockets available.
Only available on Android and iOS.
Returns: Promise<GetSocketsResult>
Since: 5.0.0
--------------------
`typescript`
joinGroup(options: JoinGroupOptions) => Promise
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
--------------------
`typescript`
leaveGroup(options: LeaveGroupOptions) => Promise
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
--------------------
`typescript`
getJoinedGroups() => Promise
Get joined groups
Only available on Android and iOS.
Returns: Promise<GetJoinedGroupsResult>
Since: 5.0.0
--------------------
`typescript`
setMulticastTimeToLive(options: SetMulticastTimeToLiveOptions) => Promise
Set the time to live (TTL) for multicast packets
Only available on Android and iOS.
| Param | Type |
| ------------- | --------------------------------------------------------------------------------------- |
| options | SetMulticastTimeToLiveOptions |
Since: 5.0.0
--------------------
`typescript`
setMulticastLoopbackMode(options: SetMulticastLoopbackModeOptions) => Promise
Set whether to enable multicast loopback mode
Only available on Android and iOS.
| Param | Type |
| ------------- | ------------------------------------------------------------------------------------------- |
| options | SetMulticastLoopbackModeOptions |
Since: 5.0.0
--------------------
`typescript`
addListener(eventName: 'receive', listenerFunc: (event: ReceiveEvent) => void) => Promise
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
--------------------
`typescript`
addListener(eventName: 'receiveError', listenerFunc: (event: ReceiveEvent) => void) => Promise
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
--------------------
#### 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 |