diff --git a/package.json b/package.json index 50b161f..16abd54 100644 --- a/package.json +++ b/package.json @@ -59,7 +59,7 @@ }, "devDependencies": { "@aws-sdk/client-s3": "^3.1002.0", - "@kevisual/api": "^0.0.60", + "@kevisual/api": "^0.0.62", "@kevisual/cnb": "^0.0.33", "@kevisual/context": "^0.0.8", "@kevisual/convex": "^0.0.4", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 175da57..d899dd8 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -66,8 +66,8 @@ importers: specifier: ^3.1002.0 version: 3.1002.0 '@kevisual/api': - specifier: ^0.0.60 - version: 0.0.60(react-dom@19.2.4(react@19.2.4))(react@19.2.4) + specifier: ^0.0.62 + version: 0.0.62(react-dom@19.2.4(react@19.2.4))(react@19.2.4) '@kevisual/cnb': specifier: ^0.0.33 version: 0.0.33(dotenv@17.3.1)(ioredis@5.9.3) @@ -863,8 +863,8 @@ packages: '@kevisual/ai@0.0.26': resolution: {integrity: sha512-lhaMpxi+vgqPdyBKiuNbSil4hy13tNLbDiqCtG0qUXKtvoowK6xMx269pSSYkYBivczM8g8I0XEouuJceUpJPg==} - '@kevisual/api@0.0.60': - resolution: {integrity: sha512-NTFDx1ns/iGli2fUJLJZRWu8nf5VkXV+sOQUqGGAJvrvGATvXSuITu6mD4P/aDQakx4hzQUPr9wDTZoNk7+RqQ==} + '@kevisual/api@0.0.62': + resolution: {integrity: sha512-GB8Ho2absXoXoZP2GKyuoRqRqjdwtV0JR512DXBaKJR2sIPn1KvuglbBiX+zPjDBBskv/ApvZKOoSwj1OmkrKQ==} '@kevisual/auth@2.0.3': resolution: {integrity: sha512-4xpijaIhlCTr/DlJaV/gmkCQeg45EO1yxWpRvUX+1jCdVbuxSR0wZrF0SD9oybnjmKWMKDNPLsXyduFjMGcItA==} @@ -3494,7 +3494,7 @@ snapshots: '@kevisual/permission': 0.0.4 '@kevisual/query': 0.0.52 - '@kevisual/api@0.0.60(react-dom@19.2.4(react@19.2.4))(react@19.2.4)': + '@kevisual/api@0.0.62(react-dom@19.2.4(react@19.2.4))(react@19.2.4)': dependencies: '@kevisual/context': 0.0.8 '@kevisual/js-filter': 0.0.5 diff --git a/src/modules/v1-ws-proxy/manager.ts b/src/modules/v1-ws-proxy/manager.ts index 18bc800..4931a7e 100644 --- a/src/modules/v1-ws-proxy/manager.ts +++ b/src/modules/v1-ws-proxy/manager.ts @@ -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); } \ No newline at end of file diff --git a/src/modules/v1-ws-proxy/proxy.ts b/src/modules/v1-ws-proxy/proxy.ts index 8b3dcd2..755158c 100644 --- a/src/modules/v1-ws-proxy/proxy.ts +++ b/src/modules/v1-ws-proxy/proxy.ts @@ -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 } }));