getting there

This commit is contained in:
eskimo 2024-10-03 13:45:22 -04:00
parent de6b28ab43
commit 7e77833452
8 changed files with 3657 additions and 21 deletions

View File

@ -13,25 +13,110 @@ npx cap sync
<docgen-index> <docgen-index>
* [`echo(...)`](#echo) * [`create(...)`](#create)
* [`connect(...)`](#connect)
* [`send(...)`](#send)
* [`disconnect(...)`](#disconnect)
* [`addListener('state', ...)`](#addlistenerstate-)
* [`addListener('message', ...)`](#addlistenermessage-)
* [Interfaces](#interfaces)
</docgen-index> </docgen-index>
<docgen-api> <docgen-api>
<!--Update the source file JSDoc comments and rerun docgen to update the docs below--> <!--Update the source file JSDoc comments and rerun docgen to update the docs below-->
### echo(...) ### create(...)
```typescript ```typescript
echo(options: { value: string; }) => Promise<{ value: string; }> create(options: { id: string; host: string; port: number; useTLS?: boolean; acceptInvalidCertificates?: boolean; }) => Promise<void>
``` ```
| Param | Type | | Param | Type |
| ------------- | ------------------------------- | | ------------- | --------------------------------------------------------------------------------------------------------------- |
| **`options`** | <code>{ value: string; }</code> | | **`options`** | <code>{ id: string; host: string; port: number; useTLS?: boolean; acceptInvalidCertificates?: boolean; }</code> |
**Returns:** <code>Promise&lt;{ value: string; }&gt;</code>
-------------------- --------------------
### connect(...)
```typescript
connect(options: { id: string; }) => Promise<void>
```
| Param | Type |
| ------------- | ---------------------------- |
| **`options`** | <code>{ id: string; }</code> |
--------------------
### send(...)
```typescript
send(options: { id: string; message: string; }) => Promise<void>
```
| Param | Type |
| ------------- | --------------------------------------------- |
| **`options`** | <code>{ id: string; message: string; }</code> |
--------------------
### disconnect(...)
```typescript
disconnect(options: { id: string; }) => Promise<void>
```
| Param | Type |
| ------------- | ---------------------------- |
| **`options`** | <code>{ id: string; }</code> |
--------------------
### addListener('state', ...)
```typescript
addListener(eventName: 'state', listenerFunc: (message: { id: string; state: string; }) => void) => Promise<PluginListenerHandle>
```
| Param | Type |
| ------------------ | ----------------------------------------------------------------- |
| **`eventName`** | <code>'state'</code> |
| **`listenerFunc`** | <code>(message: { id: string; state: string; }) =&gt; void</code> |
**Returns:** <code>Promise&lt;<a href="#pluginlistenerhandle">PluginListenerHandle</a>&gt;</code>
--------------------
### addListener('message', ...)
```typescript
addListener(eventName: 'message', listenerFunc: (message: { id: string; message: string; }) => void) => Promise<PluginListenerHandle>
```
| Param | Type |
| ------------------ | ------------------------------------------------------------------- |
| **`eventName`** | <code>'message'</code> |
| **`listenerFunc`** | <code>(message: { id: string; message: string; }) =&gt; void</code> |
**Returns:** <code>Promise&lt;<a href="#pluginlistenerhandle">PluginListenerHandle</a>&gt;</code>
--------------------
### Interfaces
#### PluginListenerHandle
| Prop | Type |
| ------------ | ----------------------------------------- |
| **`remove`** | <code>() =&gt; Promise&lt;void&gt;</code> |
</docgen-api> </docgen-api>

View File

@ -80,7 +80,7 @@ public class Socket: NSObject {
func receive(on connection: NWConnection) { func receive(on connection: NWConnection) {
connection.receive(minimumIncompleteLength: 1, maximumLength: 65536) { (data, contentContext, isComplete, error) in connection.receive(minimumIncompleteLength: 1, maximumLength: 65536) { (data, contentContext, isComplete, error) in
if let data = data, !data.isEmpty { if let data = data, !data.isEmpty {
if let message = String(data: data, encoding: .utf8)?.trimmingCharacters(in: .whitespacesAndNewlines) { if let message = String(data: data, encoding: .utf8) {
self.delegate?.didReceiveMessage(socket: self.id, message: message) self.delegate?.didReceiveMessage(socket: self.id, message: message)
} }
} }

3428
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

View File

@ -76,5 +76,8 @@
"android": { "android": {
"src": "android" "src": "android"
} }
},
"dependencies": {
"mitt": "^3.0.1"
} }
} }

View File

@ -7,6 +7,7 @@ export default {
name: 'capacitorSockets', name: 'capacitorSockets',
globals: { globals: {
'@capacitor/core': 'capacitorExports', '@capacitor/core': 'capacitorExports',
"mitt": "mitt"
}, },
sourcemap: true, sourcemap: true,
inlineDynamicImports: true, inlineDynamicImports: true,
@ -18,5 +19,5 @@ export default {
inlineDynamicImports: true, inlineDynamicImports: true,
}, },
], ],
external: ['@capacitor/core'], external: ['@capacitor/core', "mitt"],
}; };

View File

@ -1,3 +1,11 @@
import { PluginListenerHandle } from "@capacitor/core";
export interface SocketsPlugin { export interface SocketsPlugin {
echo(options: { value: string }): Promise<{ value: string }>; create(options: { id: string; host: string; port: number, useTLS?: boolean, acceptInvalidCertificates?: boolean }): Promise<void>;
connect(options: { id: string; }): Promise<void>;
send(options: { id: string; message: string }): Promise<void>;
disconnect(options: { id: string }): Promise<void>;
addListener(eventName: "state", listenerFunc: (message: { id: string; state: string }) => void): Promise<PluginListenerHandle>;
addListener(eventName: "message", listenerFunc: (message: { id: string; message: string }) => void): Promise<PluginListenerHandle>;
} }

View File

@ -1,10 +1,2 @@
import { registerPlugin } from '@capacitor/core'; import Socket from "./socket";
export { Socket };
import type { SocketsPlugin } from './definitions';
const Sockets = registerPlugin<SocketsPlugin>('Sockets', {
web: () => import('./web').then((m) => new m.SocketsWeb()),
});
export * from './definitions';
export { Sockets };

119
src/socket.ts Normal file
View File

@ -0,0 +1,119 @@
import { registerPlugin } from "@capacitor/core";
import type { SocketsPlugin } from "./definitions";
import mitt, { Emitter } from "mitt";
const Sockets = registerPlugin<SocketsPlugin>("Sockets");
type SocketEvents = {
message: string;
state: string;
};
class SocketManager {
private static instance: SocketManager;
private socketsMap: Map<string, Socket> = new Map();
private constructor() {
this.addGlobalListeners();
}
public static getInstance() {
if (!SocketManager.instance) {
SocketManager.instance = new SocketManager();
}
return SocketManager.instance;
}
public registerSocket(socket: Socket) {
this.socketsMap.set(socket.id, socket);
}
public unregisterSocket(socket: Socket) {
this.socketsMap.delete(socket.id);
}
private addGlobalListeners() {
Sockets.addListener("state", (data) => {
let socket = this.socketsMap.get(data.id);
if (socket) {
socket.didChangeState(data);
}
});
Sockets.addListener("message", (data) => {
let socket = this.socketsMap.get(data.id);
if (socket) {
socket.didReceiveMessage(data);
}
});
}
}
export default class Socket {
id: string;
host: string;
port: number;
useTLS: boolean;
acceptInvalidCertificates: boolean;
private emitter: Emitter<SocketEvents>;
constructor(config: { id: string; host: string; port: number; useTLS?: boolean; acceptInvalidCertificates?: boolean }) {
console.log(config);
this.id = config.id;
this.host = config.host;
this.port = config.port;
this.useTLS = config.useTLS ?? false;
this.acceptInvalidCertificates = config.acceptInvalidCertificates ?? false;
this.emitter = mitt<SocketEvents>();
this.create();
SocketManager.getInstance().registerSocket(this);
}
on(event: keyof SocketEvents, handler: (data: any) => void) {
this.emitter.on(event, handler);
}
create() {
Sockets.create({
id: this.id,
host: this.host,
port: this.port,
useTLS: this.useTLS,
acceptInvalidCertificates: this.acceptInvalidCertificates,
});
}
connect() {
Sockets.connect({
id: this.id,
});
}
disconnect() {
Sockets.disconnect({
id: this.id,
});
SocketManager.getInstance().unregisterSocket(this);
}
send(message: string) {
Sockets.send({
id: this.id,
message: message
});
}
didChangeState(data: { state: string }) {
this.emitter.emit("state", data.state);
}
didReceiveMessage(data: { message: string }) {
this.emitter.emit("message", data.message);
}
}