getting there
This commit is contained in:
parent
de6b28ab43
commit
7e77833452
99
README.md
99
README.md
@ -13,25 +13,110 @@ npx cap sync
|
||||
|
||||
<docgen-index>
|
||||
|
||||
* [`echo(...)`](#echo)
|
||||
* [`create(...)`](#create)
|
||||
* [`connect(...)`](#connect)
|
||||
* [`send(...)`](#send)
|
||||
* [`disconnect(...)`](#disconnect)
|
||||
* [`addListener('state', ...)`](#addlistenerstate-)
|
||||
* [`addListener('message', ...)`](#addlistenermessage-)
|
||||
* [Interfaces](#interfaces)
|
||||
|
||||
</docgen-index>
|
||||
|
||||
<docgen-api>
|
||||
<!--Update the source file JSDoc comments and rerun docgen to update the docs below-->
|
||||
|
||||
### echo(...)
|
||||
### create(...)
|
||||
|
||||
```typescript
|
||||
echo(options: { value: string; }) => Promise<{ value: string; }>
|
||||
create(options: { id: string; host: string; port: number; useTLS?: boolean; acceptInvalidCertificates?: boolean; }) => Promise<void>
|
||||
```
|
||||
|
||||
| Param | Type |
|
||||
| ------------- | ------------------------------- |
|
||||
| **`options`** | <code>{ value: string; }</code> |
|
||||
|
||||
**Returns:** <code>Promise<{ value: string; }></code>
|
||||
| ------------- | --------------------------------------------------------------------------------------------------------------- |
|
||||
| **`options`** | <code>{ id: string; host: string; port: number; useTLS?: boolean; acceptInvalidCertificates?: boolean; }</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; }) => void</code> |
|
||||
|
||||
**Returns:** <code>Promise<<a href="#pluginlistenerhandle">PluginListenerHandle</a>></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; }) => void</code> |
|
||||
|
||||
**Returns:** <code>Promise<<a href="#pluginlistenerhandle">PluginListenerHandle</a>></code>
|
||||
|
||||
--------------------
|
||||
|
||||
|
||||
### Interfaces
|
||||
|
||||
|
||||
#### PluginListenerHandle
|
||||
|
||||
| Prop | Type |
|
||||
| ------------ | ----------------------------------------- |
|
||||
| **`remove`** | <code>() => Promise<void></code> |
|
||||
|
||||
</docgen-api>
|
||||
|
@ -80,7 +80,7 @@ public class Socket: NSObject {
|
||||
func receive(on connection: NWConnection) {
|
||||
connection.receive(minimumIncompleteLength: 1, maximumLength: 65536) { (data, contentContext, isComplete, error) in
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
3428
package-lock.json
generated
Normal file
3428
package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
@ -76,5 +76,8 @@
|
||||
"android": {
|
||||
"src": "android"
|
||||
}
|
||||
},
|
||||
"dependencies": {
|
||||
"mitt": "^3.0.1"
|
||||
}
|
||||
}
|
||||
|
@ -7,6 +7,7 @@ export default {
|
||||
name: 'capacitorSockets',
|
||||
globals: {
|
||||
'@capacitor/core': 'capacitorExports',
|
||||
"mitt": "mitt"
|
||||
},
|
||||
sourcemap: true,
|
||||
inlineDynamicImports: true,
|
||||
@ -18,5 +19,5 @@ export default {
|
||||
inlineDynamicImports: true,
|
||||
},
|
||||
],
|
||||
external: ['@capacitor/core'],
|
||||
external: ['@capacitor/core', "mitt"],
|
||||
};
|
||||
|
@ -1,3 +1,11 @@
|
||||
import { PluginListenerHandle } from "@capacitor/core";
|
||||
|
||||
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>;
|
||||
}
|
||||
|
12
src/index.ts
12
src/index.ts
@ -1,10 +1,2 @@
|
||||
import { registerPlugin } from '@capacitor/core';
|
||||
|
||||
import type { SocketsPlugin } from './definitions';
|
||||
|
||||
const Sockets = registerPlugin<SocketsPlugin>('Sockets', {
|
||||
web: () => import('./web').then((m) => new m.SocketsWeb()),
|
||||
});
|
||||
|
||||
export * from './definitions';
|
||||
export { Sockets };
|
||||
import Socket from "./socket";
|
||||
export { Socket };
|
||||
|
119
src/socket.ts
Normal file
119
src/socket.ts
Normal 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);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user