chore: update version to 0.0.52, add eventemitter3 dependency, and refactor WebSocket handling

This commit is contained in:
2025-12-30 18:32:16 +08:00
parent 8b16bc2218
commit 5dcc678ad4
6 changed files with 23 additions and 12 deletions

View File

@@ -3,7 +3,7 @@ import { handleServer } from './handle-server.ts';
import * as cookie from './cookie.ts';
import { ServerType, Listener, OnListener, ServerOpts, OnWebSocketOptions, OnWebSocketFn, WebSocketListenerFun, ListenerFun, HttpListenerFun, WS } from './server-type.ts';
import { parseIfJson } from '../utils/parse.ts';
import { EventEmitter } from 'events';
import { EventEmitter } from 'eventemitter3';
type CookieFn = (name: string, value: string, options?: cookie.SerializeOptions, end?: boolean) => void;
export type HandleCtx = {
@@ -273,6 +273,10 @@ export class ServerBase implements ServerType {
end({ code: 500, message: `${type} server is error` });
}
}
/**
* 根据emitter提醒关闭ws连接
* @param ws
*/
async onWsClose(ws: WS) {
const id = ws?.data?.id || '';
if (id) {

View File

@@ -253,6 +253,7 @@ export class BunServer extends ServerBase implements ServerType {
},
close: (ws: any) => {
// WebSocket 连接关闭
ws?.data?.close?.();
this.onWsClose(ws);
},
},

View File

@@ -1,4 +1,4 @@
import EventEmitter from 'node:events';
import { EventEmitter } from 'eventemitter3';
import * as http from 'node:http';

View File

@@ -2,7 +2,6 @@
import { WebSocketServer } from 'ws';
import type { WebSocket } from 'ws';
import { ServerType } from './server-type.ts'
import { parseIfJson } from '../utils/parse.ts';
import { isBun } from '../utils/is-engine.ts';
@@ -62,6 +61,8 @@ export class WsServerBase {
});
this.server.sendConnected(ws);
this.wss.on('close', () => {
// @ts-ignore
ws?.data?.close?.();
this.server.onWsClose(ws);
});
});