ws
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"$schema": "https://json.schemastore.org/package",
|
||||
"name": "@kevisual/router",
|
||||
"version": "0.0.84",
|
||||
"version": "0.0.85",
|
||||
"description": "",
|
||||
"type": "module",
|
||||
"main": "./dist/router.js",
|
||||
|
||||
@@ -284,7 +284,7 @@ export class ServerBase implements ServerType {
|
||||
* @param ws
|
||||
*/
|
||||
async onWsClose(ws: WS) {
|
||||
const id = ws?.data?.id || '';
|
||||
const id = ws?.wsId || '';
|
||||
if (id) {
|
||||
this.emitter.emit('close--' + id, { type: 'close', ws, id });
|
||||
setTimeout(() => {
|
||||
@@ -298,4 +298,7 @@ export class ServerBase implements ServerType {
|
||||
if (this.showConnected)
|
||||
ws.send(JSON.stringify({ type: 'connected' }));
|
||||
}
|
||||
createId() {
|
||||
return Math.random().toString(36).substring(2, 15);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
* @tags bun, server, websocket, http
|
||||
* @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';
|
||||
|
||||
export class BunServer extends ServerBase implements ServerType {
|
||||
@@ -264,10 +264,14 @@ export class BunServer extends ServerBase implements ServerType {
|
||||
open: (ws: any) => {
|
||||
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 token = ws.data.token || '';
|
||||
const id = ws.data.id || '';
|
||||
if (!ws.wsId) {
|
||||
ws.wsId = this.createId();
|
||||
}
|
||||
await this.onWebSocket({ ws, message, pathname, token, id });
|
||||
},
|
||||
close: (ws: any) => {
|
||||
|
||||
@@ -49,16 +49,33 @@ export type OnWebSocketOptions<T = {}> = {
|
||||
message: string | Buffer;
|
||||
pathname: string,
|
||||
token?: string,
|
||||
/** data 的id提取出来 */
|
||||
id?: string,
|
||||
}
|
||||
export type OnWebSocketFn = (options: OnWebSocketOptions) => Promise<void> | void;
|
||||
export type WS<T = {}> = {
|
||||
send: (data: any) => void;
|
||||
close: (code?: number, reason?: string) => void;
|
||||
/**
|
||||
* ws 自己生成的一个id,主要是为了区分不同的ws连接,方便在onWebSocket中使用
|
||||
*/
|
||||
wsId?: string;
|
||||
data?: {
|
||||
/**
|
||||
* ws连接时的url,包含pathname和searchParams
|
||||
*/
|
||||
url: URL;
|
||||
/**
|
||||
* ws连接时的pathname
|
||||
*/
|
||||
pathname: string;
|
||||
/**
|
||||
* ws连接时的url中的token参数
|
||||
*/
|
||||
token?: string;
|
||||
/**
|
||||
* ws连接时的url中的id参数.
|
||||
*/
|
||||
id?: string;
|
||||
/**
|
||||
* 鉴权后的获取的信息
|
||||
|
||||
@@ -56,6 +56,11 @@ export class WsServerBase {
|
||||
token,
|
||||
id,
|
||||
}
|
||||
// @ts-ignore
|
||||
if (!ws.wsId) {
|
||||
// @ts-ignore
|
||||
ws.wsId = this.createId();
|
||||
}
|
||||
ws.on('message', async (message: string | Buffer) => {
|
||||
await this.server.onWebSocket({ ws, message, pathname, token, id });
|
||||
});
|
||||
@@ -66,7 +71,9 @@ export class WsServerBase {
|
||||
this.server.onWsClose(ws);
|
||||
});
|
||||
});
|
||||
|
||||
}
|
||||
createId() {
|
||||
return Math.random().toString(36).substring(2, 15);
|
||||
}
|
||||
}
|
||||
// TODO: ws handle and path and routerContext
|
||||
|
||||
Reference in New Issue
Block a user