From af516bd4e217c121c2ccc60437fde7c5fd7b7490 Mon Sep 17 00:00:00 2001 From: Pulkit Singh Date: Fri, 5 Jul 2024 12:13:08 +0530 Subject: [PATCH] client ipc --- package-lock.json | 16 +++++++++++++--- package.json | 1 + src/ipc/channels.enum.ts | 4 ++++ src/ipc/client-ipc.ts | 25 ++++++++++++++++--------- tsconfig.json | 5 +++-- 5 files changed, 37 insertions(+), 14 deletions(-) diff --git a/package-lock.json b/package-lock.json index 1ee54ae..dc851ce 100755 --- a/package-lock.json +++ b/package-lock.json @@ -9,6 +9,7 @@ "version": "0.1.0", "license": "MIT", "dependencies": { + "react": "^18.3.1", "socket.io-client": "^4.7.5" }, "devDependencies": { @@ -8592,8 +8593,7 @@ "node_modules/js-tokens": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/js-tokens/-/js-tokens-4.0.0.tgz", - "integrity": "sha512-RdJUflcE3cUzKiMqQgsCu06FPu9UdIJO0beYbPhHN4k6apgJtifcoCtT9bcxOpYBtpD2kCM6Sbzg4CausW/PKQ==", - "dev": true + "integrity": "sha512-RdJUflcE3cUzKiMqQgsCu06FPu9UdIJO0beYbPhHN4k6apgJtifcoCtT9bcxOpYBtpD2kCM6Sbzg4CausW/PKQ==" }, "node_modules/js-yaml": { "version": "3.14.1", @@ -9029,7 +9029,6 @@ "version": "1.4.0", "resolved": "https://registry.npmjs.org/loose-envify/-/loose-envify-1.4.0.tgz", "integrity": "sha512-lyuxPGr/Wfhrlem2CL/UcnUc1zcqKAImBDzukY7Y5F/yQiNdko6+fRLevlw1HgMySw7f611UIY408EtxRSoK3Q==", - "dev": true, "dependencies": { "js-tokens": "^3.0.0 || ^4.0.0" }, @@ -10169,6 +10168,17 @@ "safe-buffer": "^5.1.0" } }, + "node_modules/react": { + "version": "18.3.1", + "resolved": "https://registry.npmjs.org/react/-/react-18.3.1.tgz", + "integrity": "sha512-wS+hAgJShR0KhEvPJArfuPVN1+Hz1t0Y6n5jLrGQbkb4urgPE/0Rve+1kMB1v/oWgHgm4WIcV+i7F2pTVj+2iQ==", + "dependencies": { + "loose-envify": "^1.1.0" + }, + "engines": { + "node": ">=0.10.0" + } + }, "node_modules/react-is": { "version": "16.13.1", "resolved": "https://registry.npmjs.org/react-is/-/react-is-16.13.1.tgz", diff --git a/package.json b/package.json index 9be7dba..40b4478 100755 --- a/package.json +++ b/package.json @@ -52,6 +52,7 @@ "typescript": "^3.9.10" }, "dependencies": { + "react": "^18.3.1", "socket.io-client": "^4.7.5" } } diff --git a/src/ipc/channels.enum.ts b/src/ipc/channels.enum.ts index 1b1faae..fea3cb9 100644 --- a/src/ipc/channels.enum.ts +++ b/src/ipc/channels.enum.ts @@ -6,4 +6,8 @@ export enum ClientToServerChannel { export enum ServerToClientChannel { SendMessage = 'serverToClient.sendMessage', SendChatHistory = 'serverToClient.sendChatHistory', + progress = 'progress', + result = 'result', + error = 'error', + complete = 'complete' } diff --git a/src/ipc/client-ipc.ts b/src/ipc/client-ipc.ts index 042778e..994a9f6 100644 --- a/src/ipc/client-ipc.ts +++ b/src/ipc/client-ipc.ts @@ -1,5 +1,6 @@ import { ClientToServerChannel, ServerToClientChannel } from "./channels.enum"; import { ChannelBody } from "./channels.type"; +import { Socket, io } from "socket.io-client"; export class ClientIPC { private static _instance?: ClientIPC; @@ -7,13 +8,16 @@ export class ClientIPC { channel: ServerToClientChannel, callback: (body: ChannelBody) => void }[]; + private _socket?: Socket; private constructor( - private sendMessage: (channel: ClientToServerChannel, body: ChannelBody) => void, - private onMessage: (cb: (channel: ServerToClientChannel, body: ChannelBody) => void) => void + url: string, ) { this._listeners = []; - this.onMessage((channel: ServerToClientChannel, body: ChannelBody) => { + console.log("Client IPC created", url); + const newSocket = io(url) + this._socket = newSocket; + this._socket.onAny((channel: ServerToClientChannel, body: ChannelBody) => { console.log({ dataOnServerSide: body }); this._listeners.forEach((listener) => { if (listener.channel === channel) { @@ -23,21 +27,24 @@ export class ClientIPC { }); } - static getInstance( - sendMessage: (channel: ClientToServerChannel, body: ChannelBody) => void, - onMessage: (cb: (channel: ServerToClientChannel, body: ChannelBody) => void) => void - ): ClientIPC { + static getInstance(url: string): ClientIPC { + console.log({ instance: ClientIPC._instance }) if (!ClientIPC._instance) { - ClientIPC._instance = new ClientIPC(sendMessage, onMessage); + ClientIPC._instance = new ClientIPC(url); } return ClientIPC._instance; } sendToServer(channel: T, body: ChannelBody): void { - this.sendMessage(channel, body); + console.log({ dataOnClientSide: body }); + this._socket?.emit(channel, body); } onServerMessage(channel: T, callback: (body: ChannelBody) => void): void { this._listeners.push({ channel, callback: callback as any }); } + + disconnect() { + this._socket?.disconnect(); + } } diff --git a/tsconfig.json b/tsconfig.json index 2c85b2d..37a2d24 100755 --- a/tsconfig.json +++ b/tsconfig.json @@ -15,10 +15,11 @@ "strict": true, // linter checks for common issues "noImplicitReturns": true, + "noImplicitAny": false, "noFallthroughCasesInSwitch": true, // noUnused* overlap with @typescript-eslint/no-unused-vars, can disable if duplicative - "noUnusedLocals": true, - "noUnusedParameters": true, + "noUnusedLocals": false, + "noUnusedParameters": false, // use Node's module resolution algorithm, instead of the legacy TS one "moduleResolution": "node", // transpile JSX to React.createElement