From bd0ce0058e30c37b6f69da07d1e7abf7ee7d3fff Mon Sep 17 00:00:00 2001 From: abearxiong Date: Thu, 12 Mar 2026 23:21:01 +0800 Subject: [PATCH] update --- assistant/src/module/remote-app/package.json | 2 +- assistant/src/module/remote-app/remote-app.ts | 26 +++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/assistant/src/module/remote-app/package.json b/assistant/src/module/remote-app/package.json index ce30b9d..97bfad4 100644 --- a/assistant/src/module/remote-app/package.json +++ b/assistant/src/module/remote-app/package.json @@ -1,6 +1,6 @@ { "name": "@kevisual/remote-app", - "version": "0.0.6", + "version": "0.0.7", "description": "", "main": "dist/app.js", "scripts": { diff --git a/assistant/src/module/remote-app/remote-app.ts b/assistant/src/module/remote-app/remote-app.ts index 01c161e..2e2c47c 100644 --- a/assistant/src/module/remote-app/remote-app.ts +++ b/assistant/src/module/remote-app/remote-app.ts @@ -41,6 +41,8 @@ export class RemoteApp { reconnectAttempts: number = 0; reconnectTimer: NodeJS.Timeout | null = null; isManuallyClosed: boolean = false; + isInitializing: boolean = false; + private initId: number = 0; constructor(opts?: RemoteAppOptions) { this.mainApp = opts?.app; const token = opts.token; @@ -115,10 +117,18 @@ export class RemoteApp { return wsURL; } async init() { + // 防止重复初始化 + if (this.isInitializing) { + return; + } + this.isInitializing = true; + const currentInitId = ++this.initId; if (!this.url) { + this.isInitializing = false; throw new Error('No url provided for remote app'); } if (!this.id) { + this.isInitializing = false; throw new Error('No id provided for remote app'); } this.isError = false; @@ -129,11 +139,22 @@ export class RemoteApp { const ws = new WebSocket(this.getWsURL(this.url)); const that = this; ws.onopen = function () { + // 检查是否是最新的连接 + if (currentInitId !== that.initId) { + ws.close(); + return; + } that.isConnected = true; + that.isInitializing = false; that.onOpen(); console.log('[remote-app] WebSocket connection opened'); }; ws.onclose = function () { + // 检查是否是最新的连接 + if (currentInitId !== that.initId) { + return; + } + that.isInitializing = false; that.isConnected = false; that.onClose(); } @@ -141,6 +162,11 @@ export class RemoteApp { that.onMessage(event.data); } ws.onerror = function (error) { + // 检查是否是最新的连接 + if (currentInitId !== that.initId) { + return; + } + that.isInitializing = false; that.onError(error); } this.ws = ws;