feat: 更新@kevisual/api依赖版本至0.0.62,优化WsProxyManager和请求处理逻辑

This commit is contained in:
2026-03-06 00:05:34 +08:00
parent b31f2840b9
commit 53204291ce
4 changed files with 38 additions and 13 deletions

View File

@@ -161,6 +161,11 @@ export class WsProxyManager {
this.wssMap.delete(oldId);
this.wssMap.set(newId, value);
value.id = newId;
// @ts-ignore
if (value.ws?.data) {
// @ts-ignore
value.ws.data.userApp = newId;
}
logger.debug(`WsProxyManager changeId: ${oldId} -> ${newId}`);
}
}
@@ -218,11 +223,11 @@ export class WsProxyManager {
}, this.PING_INTERVAL);
}
async createNewConnection(opts: { ws: any; user: string, userApp: string, isLogin?: boolean }) {
const id = opts.userApp;
let id = opts.userApp;
let realId: string = id;
const isLogin = opts.isLogin || false;
const has = this.wssMap.has(id);
const registryId = '-registry-' + generateRegistryId(); // 生成一个随机六位字符串作为注册 ID
let registryId = '-registry-' + generateRegistryId(); // 生成一个随机六位字符串作为注册 ID
let isNeedVerify = !isLogin;
if (has) {
const value = this.wssMap.get(id);
@@ -236,8 +241,10 @@ export class WsProxyManager {
wsMessage.sendConnected();
return { wsMessage, isNew: false, id: id };
} else {
// 没有关闭,需要重新注册鉴权一下, 生成新的 id 连接.
isNeedVerify = true;
// 没有关闭 生成新的 id 连接.
id = id + '-mult-' + generateRegistryId(4);
realId = id;
logger.debug('之前的连接未关闭,使用新的 ID 连接 ws', id);
}
}
}
@@ -251,6 +258,6 @@ export class WsProxyManager {
}
}
// 生成一个随机六位字符串作为注册 ID
const generateRegistryId = () => {
return Math.random().toString(36).substring(2, 8);
const generateRegistryId = (len = 6) => {
return Math.random().toString(36).substring(2, 2 + len);
}

View File

@@ -123,7 +123,7 @@ const handleRequest = async (req: IncomingMessage, res: ServerResponse, opts?: {
const client = wsProxyManager.get(appId!)!;
if (!client) {
res.writeHead(404, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ message: '应用未找到' }));
res.end(JSON.stringify({ code: 404, message: '应用未找到' }));
return;
}
if (path === 'connected') {
@@ -132,6 +132,24 @@ const handleRequest = async (req: IncomingMessage, res: ServerResponse, opts?: {
res.end(JSON.stringify({ code: 200, message: '应用已连接' }));
return;
}
if (path === 'rename') {
const newId = url.searchParams.get('newId') || '';
if (!newId) {
res.writeHead(400, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ code: 400, message: 'newId 参数缺失' }));
return;
}
const wsMessage = wsProxyManager.get(newId!)!;
if (wsMessage) {
res.writeHead(400, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ code: 400, message: 'newId 已存在' }));
return;
}
wsProxyManager.changeId(appId, newId);
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ code: 200, message: '应用重命名成功' }));
return;
}
}
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ code: 200, data: { ids, infoList } }));