Refactor client routes and add IP fetching functionality

- Moved client route definitions to separate files for better organization.
- Added new route to fetch client IP addresses, supporting both IPv4 and IPv6.
- Implemented system information retrieval in the client routes.
- Updated package dependencies to their latest versions.
- Adjusted call route to prevent overwriting existing routes.
This commit is contained in:
2026-02-04 13:20:12 +08:00
parent 6212194f95
commit 5d6bd4f429
21 changed files with 363 additions and 256 deletions

View File

@@ -1,17 +1,21 @@
import { WSSManager } from './wss.ts';
import { App, Route } from '@kevisual/router'
import { WebSocketReq } from '@kevisual/router'
import { WebSocketReq, ListenProcessParams } from '@kevisual/router'
import { EventEmitter } from 'eventemitter3';
import { customAlphabet } from 'nanoid';
const letter = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
const customId = customAlphabet(letter, 16);
/**
* 实时注册的代码模块
*
* 别人通过 WebSocket 连接到此模块,发送路由列表和请求数据
*/
export class LiveCode {
wssManager: WSSManager;
app: App;
emitter: EventEmitter;
constructor(app: App) {
this.wssManager = new WSSManager({ heartbeatInterval: 5000 });
this.wssManager = new WSSManager({ heartbeatInterval: 6 * 5000 });
this.app = app;
this.emitter = new EventEmitter();
console.log('[LiveCode] 模块已初始化');
@@ -46,9 +50,9 @@ export class LiveCode {
return this.wssManager.getConnection(id)
}
async init(id: string): Promise<{ code: number, message?: string, data?: any }> {
return this.sendData({ path: 'router', key: 'list', }, id);
return this.sendData({ message: { path: 'router', key: 'list', } }, id);
}
sendData(data: any, id: string): Promise<{ code: number, message?: string, data?: any }> {
sendData(data: ListenProcessParams, id: string): Promise<{ code: number, message?: string, data?: any }> {
const reqId = customId()
const wss = this.getWss(id);
if (!wss) {
@@ -102,6 +106,7 @@ export class LiveCode {
description: route.description,
metadata: {
...route.metadata,
source: 'livecode',
liveCodeId: wid
},
middleware: ['auth'],
@@ -109,9 +114,15 @@ export class LiveCode {
const { token, cookie, ...rest } = ctx.query;
const tokenUser = ctx.state.tokernUser;
const res = await this.sendData({
id: route.id,
tokenUser,
payload: rest,
message: {
id: route.id,
payload: rest,
},
context: {
state: {
tokenUser
}
}
}, wid);
// console.log('路由响应数据:', res);
ctx.forward(res)