feat: update WebSocket connection and add system version sending functionality
- Changed WebSocket connection URL to localhost for testing. - Added a function to send system version to the server upon connection and after a delay. - Improved logging for received messages. chore: update package dependencies - Bump versions for several dependencies including @kevisual/query, lucide-react, vue, and others. - Update package manager version to pnpm@10.26.1. fix: adjust app initialization to use sessionStorage - Modified the app initialization to use a custom sessionStorage implementation backed by an LRU cache. feat: implement sessionStorage with LRU cache - Added a new module for sessionStorage that utilizes LRUCache for efficient caching.
This commit is contained in:
@@ -1,10 +1,12 @@
|
||||
import { fileProxy, httpProxy, createApiProxy, wsProxy } from '@/module/assistant/index.ts';
|
||||
import { fileProxy, httpProxy, createApiProxy, ProxyInfo } from '@/module/assistant/index.ts';
|
||||
import http from 'node:http';
|
||||
import { LocalProxy } from './local-proxy.ts';
|
||||
import { assistantConfig, 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';
|
||||
const localProxy = new LocalProxy({});
|
||||
localProxy.initFromAssistantConfig(assistantConfig);
|
||||
|
||||
@@ -175,8 +177,63 @@ export const proxyWs = () => {
|
||||
const apiProxy = assistantConfig.getCacheAssistantConfig()?.api?.proxy || [];
|
||||
const proxy = assistantConfig.getCacheAssistantConfig()?.proxy || [];
|
||||
const proxyApi = [...apiProxy, ...proxy].filter((item) => item.ws);
|
||||
log.debug('proxyApi ', proxyApi);
|
||||
wsProxy(app.server.server, {
|
||||
apiList: proxyApi,
|
||||
});
|
||||
|
||||
const demoProxy = [
|
||||
{
|
||||
path: '/api/ws/demo',
|
||||
target: 'https://kevisual.xiongxiao.me',
|
||||
pathname: '/api/router',
|
||||
ws: true,
|
||||
}
|
||||
]
|
||||
return proxyApi.map(createProxyInfo);
|
||||
};
|
||||
export const createProxyInfo = (proxyApiItem: ProxyInfo) => {
|
||||
const func: 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;
|
||||
}
|
||||
// @ts-ignore
|
||||
let _proxySocket = ws.data.proxySocket;
|
||||
if (!_proxySocket) {
|
||||
const _u = new URL(proxyApiItem.path, `${proxyApiItem.target}`);
|
||||
if (proxyApiItem.pathname) {
|
||||
_u.pathname = proxyApiItem.pathname;
|
||||
}
|
||||
const isHttps = _u.protocol === 'https:';
|
||||
const wsProtocol = isHttps ? 'wss' : 'ws';
|
||||
const wsUrl = `${wsProtocol}://${_u.host}${_u.pathname}`;
|
||||
console.log('WebSocket proxy URL', { wsUrl });
|
||||
const proxySocket = new WebSocket(wsUrl);
|
||||
proxySocket.on('open', () => {
|
||||
proxySocket.on('message', (message) => {
|
||||
ws.send(message);
|
||||
});
|
||||
});
|
||||
|
||||
proxySocket.on('error', (err) => {
|
||||
console.error(`WebSocket proxy error: ${err.message}`);
|
||||
});
|
||||
proxySocket.on('close', () => {
|
||||
console.log('WebSocket proxy closed');
|
||||
});
|
||||
emitter.once('close--' + id, () => {
|
||||
console.log('WebSocket client closed');
|
||||
proxySocket?.close?.();
|
||||
});
|
||||
// @ts-ignore
|
||||
ws.data.proxySocket = proxySocket;
|
||||
return;
|
||||
}
|
||||
console.log('ws.data', data);
|
||||
_proxySocket.send(JSON.stringify(data))
|
||||
}
|
||||
return {
|
||||
path: proxyApiItem.path,
|
||||
io: true,
|
||||
func: func
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user