https://github.com/gee1k/capacitor-udp-socket
https://github.com/gee1k/capacitor-udp-socket
Last synced: 3 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/gee1k/capacitor-udp-socket
- Owner: gee1k
- License: mit
- Created: 2023-03-18T02:16:48.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2025-04-14T04:26:38.000Z (9 months ago)
- Last Synced: 2025-09-19T15:55:12.694Z (4 months ago)
- Language: Java
- Size: 246 KB
- Stars: 11
- Watchers: 1
- Forks: 3
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
README
# capacitor-udp-socket
A UDP Socket Plugin for capacitor
Thanks [@unitree-czk](https://github.com/unitree-czk/capacitor-udp)
## Install
```bash
npm install capacitor-udp-socket
npx cap sync
```
## API
* [`create(...)`](#create)
* [`update(...)`](#update)
* [`bind(...)`](#bind)
* [`send(...)`](#send)
* [`close(...)`](#close)
* [`closeAllSockets()`](#closeallsockets)
* [`setBroadcast(...)`](#setbroadcast)
* [`setPaused(...)`](#setpaused)
* [`getInfo(...)`](#getinfo)
* [`getSockets()`](#getsockets)
* [`joinGroup(...)`](#joingroup)
* [`leaveGroup(...)`](#leavegroup)
* [`getJoinedGroups()`](#getjoinedgroups)
* [`setMulticastTimeToLive(...)`](#setmulticasttimetolive)
* [`setMulticastLoopbackMode(...)`](#setmulticastloopbackmode)
* [`addListener('receive', ...)`](#addlistenerreceive-)
* [`addListener('receiveError', ...)`](#addlistenerreceiveerror-)
* [Interfaces](#interfaces)
UDP socket plugin for Capacitor.
Only available on Android and iOS.
### create(...)
```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
--------------------
### update(...)
```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
--------------------
### bind(...)
```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
--------------------
### send(...)
```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
--------------------
### close(...)
```typescript
close(options: CloseOptions) => Promise
```
Close one socket
Only available on Android and iOS.
| Param | Type |
| ------------- | ----------------------------------------------------- |
| **`options`** | CloseOptions |
**Since:** 5.0.0
--------------------
### closeAllSockets()
```typescript
closeAllSockets() => Promise
```
Close All Sockets
Only available on Android and iOS.
**Since:** 5.0.0
--------------------
### setBroadcast(...)
```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
--------------------
### setPaused(...)
```typescript
setPaused(options: SetPausedOptions) => Promise
```
Pause receiving data.
Only available on Android and iOS.
| Param | Type |
| ------------- | ------------------------------------------------------------- |
| **`options`** | SetPausedOptions |
**Since:** 5.0.0
--------------------
### getInfo(...)
```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
--------------------
### getSockets()
```typescript
getSockets() => Promise
```
Obtain all the sockets available.
Only available on Android and iOS.
**Returns:** Promise<GetSocketsResult>
**Since:** 5.0.0
--------------------
### joinGroup(...)
```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
--------------------
### leaveGroup(...)
```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
--------------------
### getJoinedGroups()
```typescript
getJoinedGroups() => Promise
```
Get joined groups
Only available on Android and iOS.
**Returns:** Promise<GetJoinedGroupsResult>
**Since:** 5.0.0
--------------------
### setMulticastTimeToLive(...)
```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
--------------------
### setMulticastLoopbackMode(...)
```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
--------------------
### addListener('receive', ...)
```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
--------------------
### addListener('receiveError', ...)
```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
--------------------
### 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 |