fix:fix reconnect

This commit is contained in:
2026-02-05 16:51:40 +08:00
parent 8fc9605242
commit 8cda5f6be3
2 changed files with 20 additions and 19 deletions

View File

@@ -105,30 +105,27 @@ export class AssistantApp extends Manager {
maxReconnectAttempts: Infinity, // 无限重连 maxReconnectAttempts: Infinity, // 无限重连
enableBackoff: true, // 启用指数退避 enableBackoff: true, // 启用指数退避
}); });
const isConnect = await remoteApp.isConnect(); remoteApp.isConnect();
if (isConnect) {
remoteApp.listenProxy();
this.remoteIsConnected = true; this.remoteIsConnected = true;
// 监听连接成功和关闭事件 // 监听连接成功和关闭事件
remoteApp.on('open', () => { remoteApp.on('open', () => {
this.remoteIsConnected = true; this.remoteIsConnected = true;
logger.debug('链接到了远程应用服务器');
});
remoteApp.on('close', () => {
this.remoteIsConnected = false;
console.log('远程连接已关闭,自动重连机制正在处理...');
});
remoteApp.on('maxReconnectAttemptsReached', () => {
logger.error('远程应用重连达到最大次数,停止重连');
});
const appId = id; const appId = id;
const username = config?.auth.username || 'unknown'; const username = config?.auth.username || 'unknown';
const url = new URL(`/${username}/v1/${appId}`, config?.registry || 'https://kevisual.cn/'); const url = new URL(`/${username}/v1/${appId}`, config?.registry || 'https://kevisual.cn/');
this.remoteUrl = url.toString(); this.remoteUrl = url.toString();
console.log('远程地址', this.remoteUrl); logger.info('[remote-app] 远程地址', this.remoteUrl);
} else { logger.debug('链接到了远程应用服务器');
console.log('Not connected to remote app server'); remoteApp.listenProxy()
} });
remoteApp.on('close', () => {
this.remoteIsConnected = false;
logger.info('[remote-app] 远程连接已关闭,自动重连机制正在处理...');
});
remoteApp.on('maxReconnectAttemptsReached', () => {
logger.error('远程应用重连达到最大次数,停止重连');
});
this.remoteApp = remoteApp; this.remoteApp = remoteApp;
} else { } else {
if (!token) { if (!token) {

View File

@@ -85,6 +85,9 @@ export class RemoteApp {
} }
getWsURL(url: string) { getWsURL(url: string) {
const { protocol } = new URL(url); const { protocol } = new URL(url);
if (protocol.startsWith('ws')) {
return url.toString()
}
const wsProtocol = protocol === 'https:' ? 'wss:' : 'ws:'; const wsProtocol = protocol === 'https:' ? 'wss:' : 'ws:';
const wsURL = url.toString().replace(protocol, wsProtocol); const wsURL = url.toString().replace(protocol, wsProtocol);
return wsURL; return wsURL;
@@ -106,6 +109,7 @@ export class RemoteApp {
ws.onopen = function () { ws.onopen = function () {
that.isConnected = true; that.isConnected = true;
that.onOpen(); that.onOpen();
console.log('[remote-app] WebSocket connection opened');
}; };
ws.onclose = function () { ws.onclose = function () {
that.isConnected = false; that.isConnected = false;