From d5341efd554d09d71691902a626fcf8760a8280b Mon Sep 17 00:00:00 2001 From: youxia <675567585@qq.com> Date: Thu, 11 Jul 2024 02:38:50 +0700 Subject: [PATCH] feat: add close handler --- package.json | 3 ++- packages/feiyun/package.json | 2 +- packages/handler/package.json | 2 +- packages/server/package.json | 2 +- packages/server/src/IServer.ts | 1 + packages/server/src/feiyun.ts | 3 +++ packages/server/src/server.ts | 6 ++---- packages/server/src/socket.ts | 8 +++++--- pnpm-lock.yaml | 3 +++ 9 files changed, 19 insertions(+), 11 deletions(-) diff --git a/package.json b/package.json index a748d54..5cb4af7 100644 --- a/package.json +++ b/package.json @@ -17,6 +17,7 @@ }, "dependencies": { "@rollup/plugin-commonjs": "^25.0.7", - "@rollup/plugin-node-resolve": "^15.2.3" + "@rollup/plugin-node-resolve": "^15.2.3", + "eventemitter3": "^5.0.1" } } \ No newline at end of file diff --git a/packages/feiyun/package.json b/packages/feiyun/package.json index 2bcc87b..07d999c 100644 --- a/packages/feiyun/package.json +++ b/packages/feiyun/package.json @@ -1,7 +1,7 @@ { "name": "feiyun", "repository": "https://github.com/hxg2050/feiyun.git", - "version": "0.2.17", + "version": "0.2.18", "private": false, "type": "module", "main": "./src/index.ts", diff --git a/packages/handler/package.json b/packages/handler/package.json index c2cdd53..c947b03 100644 --- a/packages/handler/package.json +++ b/packages/handler/package.json @@ -1,6 +1,6 @@ { "name": "@feiyun/handler", - "version": "0.0.27", + "version": "0.0.28", "license": "MIT", "type": "module", "main": "./src/index.ts", diff --git a/packages/server/package.json b/packages/server/package.json index 2683d16..67af2ea 100644 --- a/packages/server/package.json +++ b/packages/server/package.json @@ -2,7 +2,7 @@ "name": "@feiyun/server", "repository": "https://github.com/hxg2050/feiyun.git", "private": false, - "version": "0.1.1", + "version": "0.1.2", "type": "module", "main": "./src/index.ts", "engines": { diff --git a/packages/server/src/IServer.ts b/packages/server/src/IServer.ts index b861159..84677c8 100644 --- a/packages/server/src/IServer.ts +++ b/packages/server/src/IServer.ts @@ -5,6 +5,7 @@ export interface IServer { start(): void handlerCallback?: (client: Socket, data: string) => void + closeHandlerCallback?: (client: Socket) => void /** * 绑定uid diff --git a/packages/server/src/feiyun.ts b/packages/server/src/feiyun.ts index 23fb6bf..29cce49 100644 --- a/packages/server/src/feiyun.ts +++ b/packages/server/src/feiyun.ts @@ -46,6 +46,9 @@ export class Feiyun { port: this.config.port }) this.server.start() + this.server.closeHandlerCallback = (client) => { + client.close(); + } this.server.handlerCallback = (client, data) => { const ctx = new Context() ctx.socket = client diff --git a/packages/server/src/server.ts b/packages/server/src/server.ts index 2910b83..77d481d 100644 --- a/packages/server/src/server.ts +++ b/packages/server/src/server.ts @@ -49,6 +49,7 @@ export class Server implements IServer { this.onMessage(this.clientsFromServerWebSocket.get(ws)!, str) }); this.wss.close((ws) => { + this.closeHandlerCallback?.(this.clientsFromServerWebSocket.get(ws)!) this.clientsFromServerWebSocket.delete(ws); }); // console.log('ws://127.0.0.1:' + this.config.port); @@ -65,10 +66,7 @@ export class Server implements IServer { handlerCallback?: (client: Socket, data: string) => void - private closeHandler?: (client: Socket) => void - onClose(callback: (client: Socket) => void) { - this.closeHandler = callback - } + closeHandlerCallback?: (client: Socket) => void on(path: string, handler: Handler) { this.handlers.set(path, handler) diff --git a/packages/server/src/socket.ts b/packages/server/src/socket.ts index 4349cb7..c20cb6b 100644 --- a/packages/server/src/socket.ts +++ b/packages/server/src/socket.ts @@ -1,4 +1,5 @@ import { IServer } from './IServer'; +import EventEmitter from 'eventemitter3'; const createId = (() => { let id = 0 @@ -7,11 +8,12 @@ const createId = (() => { } })(); -export class Socket { +export class Socket extends EventEmitter { + id = createId(); constructor(public server: IServer) { - + super(); } data: Record = {} @@ -45,6 +47,6 @@ export class Socket { * 断开链接 */ close() { - + this.emit('close', this) } } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index fa587e2..aaf7ad4 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -14,6 +14,9 @@ importers: '@rollup/plugin-node-resolve': specifier: ^15.2.3 version: 15.2.3(rollup@3.29.1) + eventemitter3: + specifier: ^5.0.1 + version: 5.0.1 devDependencies: '@changesets/cli': specifier: ^2.27.1