From 3ee3d4335f3e0916b122f015d130ba79d97008b3 Mon Sep 17 00:00:00 2001 From: John Smith Date: Wed, 3 Apr 2024 22:00:16 +0800 Subject: [PATCH] =?UTF-8?q?=E5=89=8D=E7=AB=AF=E7=9B=B4=E8=BF=9E=E5=BC=80?= =?UTF-8?q?=E6=94=BE=E5=B9=B3=E5=8F=B0=E6=8E=A5=E5=8F=A3=E4=BD=BF=E7=94=A8?= =?UTF-8?q?=E6=9B=B4=E4=BF=9D=E9=99=A9=E7=9A=84=E9=A1=B9=E7=9B=AE=E5=BF=83?= =?UTF-8?q?=E8=B7=B3=E7=AD=96=E7=95=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/api/chat/ChatClientDirectOpenLive.js | 72 ++++++++++--------- .../api/chat/ChatClientOfficialBase/index.js | 4 +- 2 files changed, 41 insertions(+), 35 deletions(-) diff --git a/frontend/src/api/chat/ChatClientDirectOpenLive.js b/frontend/src/api/chat/ChatClientDirectOpenLive.js index 4312a09..ecd45ed 100644 --- a/frontend/src/api/chat/ChatClientDirectOpenLive.js +++ b/frontend/src/api/chat/ChatClientDirectOpenLive.js @@ -24,7 +24,12 @@ export default class ChatClientDirectOpenLive extends ChatClientOfficialBase { } stop() { + if (this.gameHeartbeatTimerId) { + window.clearTimeout(this.gameHeartbeatTimerId) + this.gameHeartbeatTimerId = null + } this.endGame() + super.stop() } @@ -33,18 +38,13 @@ export default class ChatClientDirectOpenLive extends ChatClientOfficialBase { } async wsConnect() { - await super.wsConnect() - if (this.isDestroying) { - return + if (!this.isDestroying) { + this.msgHandler.onDebugMsg(new chatModels.DebugMsg({ + content: '开始连接房间' + })) } - if (this.gameId && this.gameHeartbeatTimerId === null) { - this.gameHeartbeatTimerId = window.setTimeout(this.onSendGameHeartbeat.bind(this), GAME_HEARTBEAT_INTERVAL) - } - - this.msgHandler.onDebugMsg(new chatModels.DebugMsg({ - content: '开始连接房间' - })) + return super.wsConnect() } onWsClose() { @@ -52,11 +52,6 @@ export default class ChatClientDirectOpenLive extends ChatClientOfficialBase { content: '房间连接已断开' })) - if (this.gameHeartbeatTimerId) { - window.clearTimeout(this.gameHeartbeatTimerId) - this.gameHeartbeatTimerId = null - } - super.onWsClose() } @@ -103,24 +98,28 @@ export default class ChatClientDirectOpenLive extends ChatClientOfficialBase { content: '开放平台关闭项目' })) + this.needInitRoom = true if (!this.gameId) { return true } + let gameId = this.gameId + // 直接丢弃将要关闭的gameId + this.gameId = null try { let res = (await axios.post('/api/open_live/end_game', { app_id: 0, - game_id: this.gameId + game_id: gameId })).data - if (res.code !== 0) { - if (res.code === 7000 || res.code === 7003) { - // 项目已经关闭了也算成功 - return true - } + // 项目已经关闭了也算成功 + if ([0, 7000, 7003].indexOf(res.code) === -1) { throw Error(`code=${res.code}, message=${res.message}, request_id=${res.request_id}`) } } catch (e) { console.error('endGame failed:', e) + this.msgHandler.onDebugMsg(new chatModels.DebugMsg({ + content: `开放平台关闭项目失败:${e}` + })) return false } return true @@ -141,22 +140,20 @@ export default class ChatClientDirectOpenLive extends ChatClientOfficialBase { // 保存一下,防止await之后gameId改变 let gameId = this.gameId try { - let res = (await axios.post('/api/open_live/game_heartbeat', { - game_id: this.gameId - })).data + let res = (await axios.post( + '/api/open_live/game_heartbeat', + { game_id: gameId }, + // 服务器有心跳合批,超时时间应该长一点 + { timeout: 15 * 1000 } + )).data if (res.code !== 0) { - console.error(`sendGameHeartbeat failed: code=${res.code}, message=${res.message}, request_id=${res.request_id}`) - this.msgHandler.onDebugMsg(new chatModels.DebugMsg({ - content: `开放平台项目心跳失败:code=${res.code}, message=${res.message}` - })) - if (res.code === 7003 && this.gameId === gameId) { // 项目异常关闭,可能是心跳超时,需要重新开启项目 + this.gameId = null this.needInitRoom = true this.discardWebsocket() } - - return false + throw Error(`code=${res.code}, message=${res.message}, request_id=${res.request_id}`) } } catch (e) { console.error('sendGameHeartbeat failed:', e) @@ -172,9 +169,9 @@ export default class ChatClientDirectOpenLive extends ChatClientOfficialBase { // 重连次数太多则重新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() } @@ -182,9 +179,17 @@ export default class ChatClientDirectOpenLive extends ChatClientOfficialBase { return this.hostServerUrlList[this.retryCount % this.hostServerUrlList.length] } + onWsOpen() { + super.onWsOpen() + + if (this.gameId && this.gameHeartbeatTimerId === null) { + this.gameHeartbeatTimerId = window.setTimeout(this.onSendGameHeartbeat.bind(this), GAME_HEARTBEAT_INTERVAL) + } + } + sendAuth() { this.msgHandler.onDebugMsg(new chatModels.DebugMsg({ - content: '已连接到房间,发送认证消息' + content: '已连接到房间,认证中' })) this.websocket.send(this.makePacket(this.authBody, base.OP_AUTH)) @@ -193,7 +198,6 @@ export default class ChatClientDirectOpenLive extends ChatClientOfficialBase { delayReconnect() { if (document.visibilityState !== 'visible') { // 不知道什么时候才能重连,先endGame吧 - this.needInitRoom = true this.endGame() } diff --git a/frontend/src/api/chat/ChatClientOfficialBase/index.js b/frontend/src/api/chat/ChatClientOfficialBase/index.js index eaafebe..077e43d 100644 --- a/frontend/src/api/chat/ChatClientOfficialBase/index.js +++ b/frontend/src/api/chat/ChatClientOfficialBase/index.js @@ -145,7 +145,9 @@ export default class ChatClientOfficialBase { onWsOpen() { this.sendAuth() - this.heartbeatTimerId = window.setInterval(this.sendHeartbeat.bind(this), HEARTBEAT_INTERVAL) + if (this.heartbeatTimerId === null) { + this.heartbeatTimerId = window.setInterval(this.sendHeartbeat.bind(this), HEARTBEAT_INTERVAL) + } this.refreshReceiveTimeoutTimer() }