From 9775e7dca60fe580ce1a114014b79384ee1a7c39 Mon Sep 17 00:00:00 2001 From: lyswhut Date: Mon, 28 Aug 2023 16:08:08 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E8=BF=9E=E6=8E=A5=E8=B6=85?= =?UTF-8?q?=E6=97=B6=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/plugins/sync/client/client.ts | 41 +++++++++++++++++++++++++++---- 1 file changed, 36 insertions(+), 5 deletions(-) diff --git a/src/plugins/sync/client/client.ts b/src/plugins/sync/client/client.ts index 453e894..25c1c5a 100644 --- a/src/plugins/sync/client/client.ts +++ b/src/plugins/sync/client/client.ts @@ -31,6 +31,7 @@ const heartbeatTools = { failedNum: 0, maxTryNum: 100000, stepMs: 3000, + connectTimeout: null as NodeJS.Timeout | null, pingTimeout: null as NodeJS.Timeout | null, delayRetryTimeout: null as NodeJS.Timeout | null, handleOpen() { @@ -50,15 +51,16 @@ const heartbeatTools = { }, 30000 + 1000) }, reConnnect() { - if (this.pingTimeout) { - clearTimeout(this.pingTimeout) - this.pingTimeout = null - } + this.clearTimeout() // client = null if (!client) return if (++this.failedNum > this.maxTryNum) { this.failedNum = 0 + sendSyncStatus({ + status: false, + message: 'Connect error', + }) throw new Error('connect error') } @@ -81,6 +83,10 @@ const heartbeatTools = { }, waitTime) }, clearTimeout() { + if (this.connectTimeout) { + clearTimeout(this.connectTimeout) + this.connectTimeout = null + } if (this.delayRetryTimeout) { clearTimeout(this.delayRetryTimeout) this.delayRetryTimeout = null @@ -92,7 +98,32 @@ const heartbeatTools = { }, connect(socket: LX.Sync.Socket) { console.log('heartbeatTools connect') + this.connectTimeout = setTimeout(() => { + this.connectTimeout = null + if (client) { + try { + client.close(SYNC_CLOSE_CODE.failed) + } catch {} + } + if (++this.failedNum > this.maxTryNum) { + this.failedNum = 0 + sendSyncStatus({ + status: false, + message: 'Connect error', + }) + throw new Error('connect error') + } + sendSyncStatus({ + status: false, + message: 'Connect timeout, try reconnect...', + }) + this.reConnnect() + }, 2 * 60 * 1000) socket.addEventListener('open', () => { + if (this.connectTimeout) { + clearTimeout(this.connectTimeout) + this.connectTimeout = null + } this.handleOpen() }) socket.addEventListener('message', ({ data }) => { @@ -128,7 +159,7 @@ export const connect = (urlInfo: LX.Sync.UrlInfo, keyInfo: LX.Sync.KeyInfo) => { funcsObj: { ...callObj, finished() { - toast('Sync connected.') + toast('Sync connected') client!.isReady = true sendSyncStatus({ status: true,