This commit is contained in:
2026-03-05 01:01:12 +08:00
parent 2628eb3693
commit e58c99c285
5 changed files with 36 additions and 5 deletions

View File

@@ -1,7 +1,7 @@
{ {
"$schema": "https://json.schemastore.org/package", "$schema": "https://json.schemastore.org/package",
"name": "@kevisual/router", "name": "@kevisual/router",
"version": "0.0.84", "version": "0.0.85",
"description": "", "description": "",
"type": "module", "type": "module",
"main": "./dist/router.js", "main": "./dist/router.js",

View File

@@ -284,7 +284,7 @@ export class ServerBase implements ServerType {
* @param ws * @param ws
*/ */
async onWsClose(ws: WS) { async onWsClose(ws: WS) {
const id = ws?.data?.id || ''; const id = ws?.wsId || '';
if (id) { if (id) {
this.emitter.emit('close--' + id, { type: 'close', ws, id }); this.emitter.emit('close--' + id, { type: 'close', ws, id });
setTimeout(() => { setTimeout(() => {
@@ -298,4 +298,7 @@ export class ServerBase implements ServerType {
if (this.showConnected) if (this.showConnected)
ws.send(JSON.stringify({ type: 'connected' })); ws.send(JSON.stringify({ type: 'connected' }));
} }
createId() {
return Math.random().toString(36).substring(2, 15);
}
} }

View File

@@ -4,7 +4,7 @@
* @tags bun, server, websocket, http * @tags bun, server, websocket, http
* @createdAt 2025-12-20 * @createdAt 2025-12-20
*/ */
import { ServerType, type ServerOpts, type Cors, RouterRes, RouterReq } from './server-type.ts'; import { ServerType, type ServerOpts, type Cors, RouterRes, RouterReq, WS } from './server-type.ts';
import { ServerBase } from './server-base.ts'; import { ServerBase } from './server-base.ts';
export class BunServer extends ServerBase implements ServerType { export class BunServer extends ServerBase implements ServerType {
@@ -264,10 +264,14 @@ export class BunServer extends ServerBase implements ServerType {
open: (ws: any) => { open: (ws: any) => {
this.sendConnected(ws); this.sendConnected(ws);
}, },
message: async (ws: any, message: string | Buffer) => { message: async (bunWs: any, message: string | Buffer) => {
const ws = bunWs as WS;
const pathname = ws.data.pathname || ''; const pathname = ws.data.pathname || '';
const token = ws.data.token || ''; const token = ws.data.token || '';
const id = ws.data.id || ''; const id = ws.data.id || '';
if (!ws.wsId) {
ws.wsId = this.createId();
}
await this.onWebSocket({ ws, message, pathname, token, id }); await this.onWebSocket({ ws, message, pathname, token, id });
}, },
close: (ws: any) => { close: (ws: any) => {

View File

@@ -49,16 +49,33 @@ export type OnWebSocketOptions<T = {}> = {
message: string | Buffer; message: string | Buffer;
pathname: string, pathname: string,
token?: string, token?: string,
/** data 的id提取出来 */
id?: string, id?: string,
} }
export type OnWebSocketFn = (options: OnWebSocketOptions) => Promise<void> | void; export type OnWebSocketFn = (options: OnWebSocketOptions) => Promise<void> | void;
export type WS<T = {}> = { export type WS<T = {}> = {
send: (data: any) => void; send: (data: any) => void;
close: (code?: number, reason?: string) => void; close: (code?: number, reason?: string) => void;
/**
* ws 自己生成的一个id主要是为了区分不同的ws连接方便在onWebSocket中使用
*/
wsId?: string;
data?: { data?: {
/**
* ws连接时的url包含pathname和searchParams
*/
url: URL; url: URL;
/**
* ws连接时的pathname
*/
pathname: string; pathname: string;
/**
* ws连接时的url中的token参数
*/
token?: string; token?: string;
/**
* ws连接时的url中的id参数.
*/
id?: string; id?: string;
/** /**
* 鉴权后的获取的信息 * 鉴权后的获取的信息

View File

@@ -56,6 +56,11 @@ export class WsServerBase {
token, token,
id, id,
} }
// @ts-ignore
if (!ws.wsId) {
// @ts-ignore
ws.wsId = this.createId();
}
ws.on('message', async (message: string | Buffer) => { ws.on('message', async (message: string | Buffer) => {
await this.server.onWebSocket({ ws, message, pathname, token, id }); await this.server.onWebSocket({ ws, message, pathname, token, id });
}); });
@@ -66,7 +71,9 @@ export class WsServerBase {
this.server.onWsClose(ws); this.server.onWsClose(ws);
}); });
}); });
}
createId() {
return Math.random().toString(36).substring(2, 15);
} }
} }
// TODO: ws handle and path and routerContext // TODO: ws handle and path and routerContext