Skip to content

Commit

Permalink
feat: add close handler
Browse files Browse the repository at this point in the history
  • Loading branch information
hxg2050 committed Jul 10, 2024
1 parent 2d9d98e commit d5341ef
Show file tree
Hide file tree
Showing 9 changed files with 19 additions and 11 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
2 changes: 1 addition & 1 deletion packages/feiyun/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion packages/handler/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@feiyun/handler",
"version": "0.0.27",
"version": "0.0.28",
"license": "MIT",
"type": "module",
"main": "./src/index.ts",
Expand Down
2 changes: 1 addition & 1 deletion packages/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
1 change: 1 addition & 0 deletions packages/server/src/IServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export interface IServer {
start(): void

handlerCallback?: (client: Socket, data: string) => void
closeHandlerCallback?: (client: Socket) => void

/**
* 绑定uid
Expand Down
3 changes: 3 additions & 0 deletions packages/server/src/feiyun.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 2 additions & 4 deletions packages/server/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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)
Expand Down
8 changes: 5 additions & 3 deletions packages/server/src/socket.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { IServer } from './IServer';
import EventEmitter from 'eventemitter3';

const createId = (() => {
let id = 0
Expand All @@ -7,11 +8,12 @@ const createId = (() => {
}
})();

export class Socket {
export class Socket extends EventEmitter {

id = createId();

constructor(public server: IServer) {

super();
}

data: Record<string, any> = {}
Expand Down Expand Up @@ -45,6 +47,6 @@ export class Socket {
* 断开链接
*/
close() {

this.emit('close', this)
}
}
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit d5341ef

Please sign in to comment.