改善刷新页面导致连接数超出限制的问题

This commit is contained in:
John Smith 2024-12-07 22:59:22 +08:00
parent 45db27d3c7
commit b73608af79

View File

@ -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)