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