From 9663a773382880920d4f010fda2b481185b0f5b2 Mon Sep 17 00:00:00 2001 From: John Smith Date: Fri, 26 Jan 2024 02:24:32 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BD=93=E9=A1=B5=E9=9D=A2=E4=B8=8D=E5=8F=AF?= =?UTF-8?q?=E8=A7=81=E6=97=B6=E6=9A=82=E6=97=B6=E4=B8=8D=E9=87=8D=E8=BF=9E?= =?UTF-8?q?=EF=BC=8C=E9=98=B2=E6=AD=A2=E9=87=8D=E8=BF=9E=E5=90=8E=E5=8F=88?= =?UTF-8?q?=E5=BF=83=E8=B7=B3=E8=B6=85=E6=97=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/api/chat/ChatClientDirectOpenLive.js | 39 ++++++++++++++----- .../api/chat/ChatClientOfficialBase/index.js | 19 ++++++++- frontend/src/api/chat/ChatClientRelay.js | 1 + 3 files changed, 48 insertions(+), 11 deletions(-) diff --git a/frontend/src/api/chat/ChatClientDirectOpenLive.js b/frontend/src/api/chat/ChatClientDirectOpenLive.js index 759a972..c54a7cd 100644 --- a/frontend/src/api/chat/ChatClientDirectOpenLive.js +++ b/frontend/src/api/chat/ChatClientDirectOpenLive.js @@ -24,24 +24,32 @@ export default class ChatClientDirectOpenLive extends ChatClientOfficialBase { } stop() { - super.stop() - - if (this.gameHeartbeatTimerId) { - window.clearTimeout(this.gameHeartbeatTimerId) - this.gameHeartbeatTimerId = null - } this.endGame() + super.stop() } async initRoom() { - if (!await this.startGame()) { - return false + return this.startGame() + } + + async wsConnect() { + await super.wsConnect() + if (this.isDestroying) { + return } if (this.gameId && this.gameHeartbeatTimerId === null) { this.gameHeartbeatTimerId = window.setTimeout(this.onSendGameHeartbeat.bind(this), GAME_HEARTBEAT_INTERVAL) } - return true + } + + onWsClose() { + if (this.gameHeartbeatTimerId) { + window.clearTimeout(this.gameHeartbeatTimerId) + this.gameHeartbeatTimerId = null + } + + super.onWsClose() } async startGame() { @@ -139,10 +147,11 @@ export default class ChatClientDirectOpenLive extends ChatClientOfficialBase { } async onBeforeWsConnect() { - // 重连次数太多则重新init_room,保险 + // 重连次数太多则重新initRoom,保险 let reinitPeriod = Math.max(3, (this.hostServerUrlList || []).length) if (this.retryCount > 0 && this.retryCount % reinitPeriod === 0) { this.needInitRoom = true + await this.endGame() } return super.onBeforeWsConnect() } @@ -155,6 +164,16 @@ export default class ChatClientDirectOpenLive extends ChatClientOfficialBase { this.websocket.send(this.makePacket(this.authBody, base.OP_AUTH)) } + delayReconnect() { + if (document.visibilityState !== 'visible') { + // 不知道什么时候才能重连,先endGame吧 + this.needInitRoom = true + this.endGame() + } + + super.delayReconnect() + } + async dmCallback(command) { let data = command.data diff --git a/frontend/src/api/chat/ChatClientOfficialBase/index.js b/frontend/src/api/chat/ChatClientOfficialBase/index.js index 948e47a..3903711 100644 --- a/frontend/src/api/chat/ChatClientOfficialBase/index.js +++ b/frontend/src/api/chat/ChatClientOfficialBase/index.js @@ -200,7 +200,24 @@ export default class ChatClientOfficialBase { return } - window.setTimeout(this.wsConnect.bind(this), this.getReconnectInterval()) + this.delayReconnect() + } + + delayReconnect() { + if (document.visibilityState === 'visible') { + window.setTimeout(this.wsConnect.bind(this), this.getReconnectInterval()) + return + } + + // 页面不可见就先不重连了,即使重连也会心跳超时 + let listener = () => { + if (document.visibilityState !== 'visible') { + return + } + document.removeEventListener('visibilitychange', listener) + this.wsConnect() + } + document.addEventListener('visibilitychange', listener) } getReconnectInterval() { diff --git a/frontend/src/api/chat/ChatClientRelay.js b/frontend/src/api/chat/ChatClientRelay.js index 54a67bf..b0430bb 100644 --- a/frontend/src/api/chat/ChatClientRelay.js +++ b/frontend/src/api/chat/ChatClientRelay.js @@ -107,6 +107,7 @@ export default class ChatClientRelay { return } + // 这边不用判断页面是否可见,因为发心跳包不是由定时器触发的,即使是不活动页面也不会心跳超时 window.setTimeout(this.wsConnect.bind(this), this.getReconnectInterval()) }