From b73608af79940a732656db3b2c7afe8b2f434147 Mon Sep 17 00:00:00 2001 From: John Smith Date: Sat, 7 Dec 2024 22:59:22 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E5=96=84=E5=88=B7=E6=96=B0=E9=A1=B5?= =?UTF-8?q?=E9=9D=A2=E5=AF=BC=E8=87=B4=E8=BF=9E=E6=8E=A5=E6=95=B0=E8=B6=85?= =?UTF-8?q?=E5=87=BA=E9=99=90=E5=88=B6=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/api/chat/ChatClientDirectOpenLive.js | 35 ++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/frontend/src/api/chat/ChatClientDirectOpenLive.js b/frontend/src/api/chat/ChatClientDirectOpenLive.js index 75a750f..d16c422 100644 --- a/frontend/src/api/chat/ChatClientDirectOpenLive.js +++ b/frontend/src/api/chat/ChatClientDirectOpenLive.js @@ -1,4 +1,4 @@ -import { apiClient as axios } from '@/api/base' +import { apiClient as axios, getBaseUrl } from '@/api/base' import * as chat from '.' import * as chatModels from './models' import * as base from './ChatClientOfficialBase' @@ -13,6 +13,8 @@ export default class ChatClientDirectOpenLive extends ChatClientOfficialBase { this.roomOwnerAuthCode = roomOwnerAuthCode + this.boundEndGameBeforeUnload = this.endGameBeforeUnload.bind(this) + // 调用initRoom后初始化 this.roomOwnerOpenId = null this.hostServerUrlList = [] @@ -22,7 +24,15 @@ export default class ChatClientDirectOpenLive extends ChatClientOfficialBase { this.gameHeartbeatTimerId = null } + start() { + super.start() + + window.addEventListener('beforeunload', this.boundEndGameBeforeUnload) + } + stop() { + window.removeEventListener('beforeunload', this.boundEndGameBeforeUnload) + this.endGame() super.stop() @@ -99,6 +109,29 @@ export default class ChatClientDirectOpenLive extends ChatClientOfficialBase { return true } + endGameBeforeUnload() { + let baseUrl = getBaseUrl() + if (baseUrl === null) { + return + } + + this.needInitRoom = true + if (!this.gameId) { + return + } + let gameId = this.gameId + // 直接丢弃将要关闭的gameId + this.gameId = null + + let url = `${baseUrl}/api/open_live/end_game` + let body = { + app_id: 0, + game_id: gameId + } + body = new Blob([JSON.stringify(body)], { type: 'application/json' }) + window.navigator.sendBeacon(url, body) + } + onSendGameHeartbeat() { // 加上随机延迟,减少同时请求的概率 let sleepTime = GAME_HEARTBEAT_INTERVAL - (2 * 1000) + (Math.random() * 3 * 1000)