From 864766be4ab8ec6522482a5d558f77423b6ee1ec Mon Sep 17 00:00:00 2001 From: xiongxiao Date: Sat, 20 Dec 2025 23:20:41 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=9B=B4=E6=96=B0=20RemoteApp=20?= =?UTF-8?q?=E7=B1=BB=E4=BB=A5=E6=94=AF=E6=8C=81=20token=20=E5=92=8C=20app?= =?UTF-8?q?=20ID=EF=BC=8C=E5=A2=9E=E5=BC=BA=20WebSocket=20URL=20=E7=94=9F?= =?UTF-8?q?=E6=88=90=E9=80=BB=E8=BE=91=EF=BC=9B=E6=B7=BB=E5=8A=A0=20ws-app?= =?UTF-8?q?=20=E6=B5=8B=E8=AF=95=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../module/assistant/remote-app/remote-app.ts | 18 +++++++--- assistant/src/module/http-token.ts | 2 +- assistant/src/test/remote-app.ts | 4 ++- assistant/src/test/ws-app.ts | 35 +++++++++++++++++++ 4 files changed, 53 insertions(+), 6 deletions(-) create mode 100644 assistant/src/test/ws-app.ts diff --git a/assistant/src/module/assistant/remote-app/remote-app.ts b/assistant/src/module/assistant/remote-app/remote-app.ts index 107f56b..2e2cc9d 100644 --- a/assistant/src/module/assistant/remote-app/remote-app.ts +++ b/assistant/src/module/assistant/remote-app/remote-app.ts @@ -20,11 +20,20 @@ export class RemoteApp { constructor(opts?: RemoteAppOptions) { this.mainApp = opts?.app; this.assistantConfig = opts?.assistantConfig; - const share = this.assistantConfig?.getConfig()?.share; + const config = this.assistantConfig?.getConfig(); + const share = config?.share; + const token = config?.token; + const app = config?.app || {}; + const name = app.id; this.emitter = opts?.emitter || new EventEmitter(); if (share) { - const { url, name, enabled } = share; - this.url = url; + const { url, enabled } = share; + const _url = new URL(url); + if (token) { + _url.searchParams.set('token', token); + } + _url.searchParams.set('id', app.id); + this.url = _url.toString(); this.name = name; this.enabled = enabled ?? false; if (this.enabled) { @@ -84,6 +93,7 @@ export class RemoteApp { this.emitter.emit('close', this.name); } onMessage(data: any) { + console.log('Message from remote app:', this.name, data.toString()); this.emitter.emit('message', data); } onError(error: any) { @@ -97,7 +107,7 @@ export class RemoteApp { }; } - sendData(data: any) {} + sendData(data: any) { } json(data: any) { this.ws.send(JSON.stringify(data)); } diff --git a/assistant/src/module/http-token.ts b/assistant/src/module/http-token.ts index 27ddd31..27b7ea7 100644 --- a/assistant/src/module/http-token.ts +++ b/assistant/src/module/http-token.ts @@ -1,4 +1,4 @@ -import http from 'http'; +import http from 'node:http'; export const error = (msg: string, code = 500) => { return JSON.stringify({ code, message: msg }); }; diff --git a/assistant/src/test/remote-app.ts b/assistant/src/test/remote-app.ts index 576671b..36bcb0c 100644 --- a/assistant/src/test/remote-app.ts +++ b/assistant/src/test/remote-app.ts @@ -1,5 +1,6 @@ import { logger } from '@/module/logger.ts'; import { assistantConfig, app } from '../app.ts'; +import { WebSocket } from 'ws'; import '../routes/index.ts'; import { RemoteApp } from '@/module/assistant/remote-app/remote-app.ts'; const main = async () => { @@ -17,4 +18,5 @@ const main = async () => { } }; -main(); +// main(); + diff --git a/assistant/src/test/ws-app.ts b/assistant/src/test/ws-app.ts new file mode 100644 index 0000000..ab25e88 --- /dev/null +++ b/assistant/src/test/ws-app.ts @@ -0,0 +1,35 @@ +import { WebSocket } from 'ws'; + +const testRouter = () => { + // const ws = new WebSocket('ws://121.4.112.18:3005/api/router'); + // const ws = new WebSocket('wss://kevisual.xiongxiao.me/ws/proxy'); + // const ws = new WebSocket('wss://kevisual.xiongxiao.me/api/router'); + + const ws = new WebSocket('ws://118.196.32.29:3005/api/router'); + // const ws = new WebSocket('wss://kevisual.cn/api/router'); + console.log('Connecting to WebSocket server...'); + ws.on('open', () => { + console.log('WebSocket connection opened'); + ws.send( + JSON.stringify({ + "type": "router", + "data": { + path: 'system', + key: 'version' + } + }), + ); + }); + + ws.on('message', (data) => { + console.log('Received message from server:', data.toString()); + }); + ws.on('close', () => { + console.log('WebSocket connection closed'); + }); + ws.on('error', (error) => { + console.error('WebSocket error:', error); + }); +} + +testRouter() \ No newline at end of file