getting there
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user