feat: Implement LiveCode module with WebSocket and SSE support

- Added config management using `useConfig` for environment variables.
- Created `LiveCode` class to manage WebSocket connections and routing.
- Implemented `SSEManager` for Server-Sent Events handling.
- Developed `WSSManager` for managing WebSocket connections with heartbeat functionality.
- Introduced `ReconnectingWebSocket` class for robust WebSocket client with automatic reconnection.
- Added test files for live application demonstrating WebSocket and TCP server integration.
This commit is contained in:
2026-02-02 23:29:58 +08:00
parent 5774391bbe
commit a76c2235ea
19 changed files with 871 additions and 385 deletions

View File

@@ -1,13 +1,14 @@
import { createApiProxy, ProxyInfo, proxy } from '@/module/assistant/index.ts';
import http from 'node:http';
import { LocalProxy } from './local-proxy.ts';
import { assistantConfig, simpleRouter } from '@/app.ts';
import { assistantConfig, simpleRouter, app } from '@/app.ts';
import { log, logger } from '@/module/logger.ts';
import { getToken } from '@/module/http-token.ts';
import { getTokenUserCache } from '@/routes/index.ts';
import type { WebSocketListenerFun } from "@kevisual/router";
import WebSocket from 'ws';
import { renderNoAuthAndLogin } from '@/module/assistant/html/login.ts';
import { LiveCode } from '@/module/livecode/index.ts';
const localProxy = new LocalProxy({});
localProxy.initFromAssistantConfig(assistantConfig);
@@ -234,6 +235,27 @@ export const proxyWs = () => {
}
return proxyApi.map(createProxyInfo);
};
const liveCode = new LiveCode(app)
export const proxyLivecodeWs = () => {
const livecode = assistantConfig.getCacheAssistantConfig()?.router?.livecode ?? true;
if (!livecode) {
return [];
}
const fun: WebSocketListenerFun = async (req, res) => {
const { ws, emitter, id, data } = req;
// if (!id) {
// ws.send(JSON.stringify({ type: 'error', message: 'not found id' }));
// ws.close();
// return;
// }
liveCode.conn(req)
}
return [{
path: '/livecode/ws',
io: true,
func: fun
}]
}
export const createProxyInfo = (proxyApiItem: ProxyInfo) => {
const func: WebSocketListenerFun = async (req, res) => {
const { ws, emitter, id, data } = req;