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