feat: 更新 RemoteApp 类,增加 isVerified 属性及验证等待逻辑;优化错误日志输出
This commit is contained in:
@@ -133,7 +133,7 @@ export class AssistantApp extends Manager {
|
|||||||
if (url && id) {
|
if (url && id) {
|
||||||
const remoteApp = new RemoteApp({
|
const remoteApp = new RemoteApp({
|
||||||
url: url.toString(),
|
url: url.toString(),
|
||||||
token,
|
// token,
|
||||||
username: config?.auth?.username || '',
|
username: config?.auth?.username || '',
|
||||||
id,
|
id,
|
||||||
app: this.mainApp,
|
app: this.mainApp,
|
||||||
|
|||||||
@@ -28,6 +28,7 @@ export class RemoteApp {
|
|||||||
username: string;
|
username: string;
|
||||||
emitter: EventEmitter;
|
emitter: EventEmitter;
|
||||||
isConnected: boolean;
|
isConnected: boolean;
|
||||||
|
isVerified: boolean;
|
||||||
ws: WebSocket;
|
ws: WebSocket;
|
||||||
remoteIsConnected: boolean;
|
remoteIsConnected: boolean;
|
||||||
isError: boolean = false;
|
isError: boolean = false;
|
||||||
@@ -85,11 +86,25 @@ export class RemoteApp {
|
|||||||
clearTimeout(timeout);
|
clearTimeout(timeout);
|
||||||
that.isConnected = true;
|
that.isConnected = true;
|
||||||
that.remoteIsConnected = true;
|
that.remoteIsConnected = true;
|
||||||
|
|
||||||
resolve(true);
|
resolve(true);
|
||||||
};
|
};
|
||||||
that.emitter.once('open', listenOnce);
|
that.emitter.once('open', listenOnce);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
async waitVerify(): Promise<boolean> {
|
||||||
|
if (this.isVerified) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
// 等待验证成功
|
||||||
|
return new Promise((resolve) => {
|
||||||
|
const listenOnce = () => {
|
||||||
|
this.isVerified = true;
|
||||||
|
resolve(true);
|
||||||
|
};
|
||||||
|
this.emitter.once('verified', listenOnce);
|
||||||
|
});
|
||||||
|
}
|
||||||
getWsURL(url: string) {
|
getWsURL(url: string) {
|
||||||
const { protocol } = new URL(url);
|
const { protocol } = new URL(url);
|
||||||
if (protocol.startsWith('ws')) {
|
if (protocol.startsWith('ws')) {
|
||||||
@@ -238,12 +253,18 @@ export class RemoteApp {
|
|||||||
const bodyData = body?.data as ListenProcessParams;
|
const bodyData = body?.data as ListenProcessParams;
|
||||||
const message = bodyData?.message || {};
|
const message = bodyData?.message || {};
|
||||||
const context = bodyData?.context || {};
|
const context = bodyData?.context || {};
|
||||||
|
console.log('[remote-app] 远程应用收到消息:', body);
|
||||||
if (body?.code === 401) {
|
if (body?.code === 401) {
|
||||||
console.error('远程应用认证失败,请检查 token 是否正确');
|
console.error('[remote-app] 远程应用认证失败,请检查 token 是否正确');
|
||||||
this.isError = true;
|
this.isError = true;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
if (body?.type === 'verified') {
|
||||||
|
remoteApp.emitter.emit('verified');
|
||||||
|
return;
|
||||||
|
}
|
||||||
if (body?.type !== 'proxy') return;
|
if (body?.type !== 'proxy') return;
|
||||||
|
|
||||||
if (!body.id) {
|
if (!body.id) {
|
||||||
remoteApp.json({
|
remoteApp.json({
|
||||||
id: body.id,
|
id: body.id,
|
||||||
@@ -265,7 +286,7 @@ export class RemoteApp {
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('处理远程代理请求出错:', error);
|
console.error('[remote-app] 处理远程代理请求出错:', error);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
remoteApp.json({
|
remoteApp.json({
|
||||||
@@ -273,7 +294,7 @@ export class RemoteApp {
|
|||||||
type: 'registryClient',
|
type: 'registryClient',
|
||||||
username: username,
|
username: username,
|
||||||
});
|
});
|
||||||
console.log(`远程应用 ${this.id} (${username}) 已注册到主应用,等待消息...`);
|
console.log(`[remote-app] 远程应用 ${this.id} (${username}) 已注册到主应用,等待消息...`);
|
||||||
remoteApp.emitter.on('message', listenFn);
|
remoteApp.emitter.on('message', listenFn);
|
||||||
const closeMessage = () => {
|
const closeMessage = () => {
|
||||||
remoteApp.emitter.off('message', listenFn);
|
remoteApp.emitter.off('message', listenFn);
|
||||||
|
|||||||
Reference in New Issue
Block a user