chore: update version to 0.0.49 and refactor WebSocket handling and type definitions

This commit is contained in:
2025-12-21 20:37:50 +08:00
parent caaec4d870
commit a76f1fb5d2
5 changed files with 28 additions and 38 deletions

View File

@@ -40,11 +40,17 @@ export interface ServerType {
*/
on(listener: OnListener): void;
onWebSocket({ ws, message, pathname, token, id }: OnWebSocketOptions): void;
onWsClose(ws: WS): void;
sendConnected(ws: WS): void;
onWsClose<T = {}>(ws: WS<T>): void;
sendConnected<T = {}>(ws: WS<T>): void;
}
export type OnWebSocketOptions = { ws: WS; message: string | Buffer; pathname: string, token?: string, id?: string }
export type OnWebSocketOptions<T = {}> = {
ws: WS<T>;
message: string | Buffer;
pathname: string,
token?: string,
id?: string,
}
export type OnWebSocketFn = (options: OnWebSocketOptions) => Promise<void> | void;
export type WS<T = {}> = {
send: (data: any) => void;
@@ -65,15 +71,20 @@ export type Listener = {
io?: boolean;
path?: string;
func: WebSocketListenerFun | HttpListenerFun;
/**
* @description 是否默认解析为 JSON如果为 true则 message 会被 JSON.parse 处理,默认是 true
*/
json?: boolean,
}
export type WebSocketListenerFun = (req: WebSocketReq, res: WebSocketRes) => Promise<void> | void;
export type HttpListenerFun = (req: RouterReq, res: RouterRes) => Promise<void> | void;
export type WebSocketReq = {
export type WebSocketReq<T = {}, U = Record<string, any>> = {
emitter?: EventEmitter;
ws: WS;
data: any;
ws: WS<T>;
data?: U;
message?: string | Buffer;
pathname?: string;
token?: string;
id?: string;