feat: 更新 RemoteApp 类以支持 token 和 app ID,增强 WebSocket URL 生成逻辑;添加 ws-app 测试文件
This commit is contained in:
@@ -20,11 +20,20 @@ export class RemoteApp {
|
|||||||
constructor(opts?: RemoteAppOptions) {
|
constructor(opts?: RemoteAppOptions) {
|
||||||
this.mainApp = opts?.app;
|
this.mainApp = opts?.app;
|
||||||
this.assistantConfig = opts?.assistantConfig;
|
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();
|
this.emitter = opts?.emitter || new EventEmitter();
|
||||||
if (share) {
|
if (share) {
|
||||||
const { url, name, enabled } = share;
|
const { url, enabled } = share;
|
||||||
this.url = url;
|
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.name = name;
|
||||||
this.enabled = enabled ?? false;
|
this.enabled = enabled ?? false;
|
||||||
if (this.enabled) {
|
if (this.enabled) {
|
||||||
@@ -84,6 +93,7 @@ export class RemoteApp {
|
|||||||
this.emitter.emit('close', this.name);
|
this.emitter.emit('close', this.name);
|
||||||
}
|
}
|
||||||
onMessage(data: any) {
|
onMessage(data: any) {
|
||||||
|
console.log('Message from remote app:', this.name, data.toString());
|
||||||
this.emitter.emit('message', data);
|
this.emitter.emit('message', data);
|
||||||
}
|
}
|
||||||
onError(error: any) {
|
onError(error: any) {
|
||||||
@@ -97,7 +107,7 @@ export class RemoteApp {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
sendData(data: any) {}
|
sendData(data: any) { }
|
||||||
json(data: any) {
|
json(data: any) {
|
||||||
this.ws.send(JSON.stringify(data));
|
this.ws.send(JSON.stringify(data));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import http from 'http';
|
import http from 'node:http';
|
||||||
export const error = (msg: string, code = 500) => {
|
export const error = (msg: string, code = 500) => {
|
||||||
return JSON.stringify({ code, message: msg });
|
return JSON.stringify({ code, message: msg });
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import { logger } from '@/module/logger.ts';
|
import { logger } from '@/module/logger.ts';
|
||||||
import { assistantConfig, app } from '../app.ts';
|
import { assistantConfig, app } from '../app.ts';
|
||||||
|
import { WebSocket } from 'ws';
|
||||||
import '../routes/index.ts';
|
import '../routes/index.ts';
|
||||||
import { RemoteApp } from '@/module/assistant/remote-app/remote-app.ts';
|
import { RemoteApp } from '@/module/assistant/remote-app/remote-app.ts';
|
||||||
const main = async () => {
|
const main = async () => {
|
||||||
@@ -17,4 +18,5 @@ const main = async () => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
main();
|
// main();
|
||||||
|
|
||||||
|
|||||||
35
assistant/src/test/ws-app.ts
Normal file
35
assistant/src/test/ws-app.ts
Normal file
@@ -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()
|
||||||
Reference in New Issue
Block a user