fix: 优化 Nakama 心跳机制以防止僵尸心跳并修复扫雷游戏结算后的误触问题

This commit is contained in:
邹方成 2026-01-03 19:02:51 +08:00
parent 46430edb8b
commit 0367a8db8c
2 changed files with 19 additions and 3 deletions

View File

@ -774,7 +774,8 @@ export default {
},
handleCellClick(idx) {
if (this.isSpectator) return;
if (!this.gameState || !this.gameState.gameStarted) return;
// showResultModal || showSettlement
if (!this.gameState || !this.gameState.gameStarted || this.showResultModal || this.showSettlement) return;
if (!this.isMyTurn) return;
if (this.gameState.grid[idx].revealed) return;
nakamaManager.sendMatchState(this.matchId, 3, JSON.stringify({ index: idx }));

View File

@ -32,6 +32,7 @@ class NakamaManager {
// 心跳定时器
this.heartbeatTimer = null;
this.heartbeatInterval = 10000; // 10秒
this.heartbeatId = 0; // 用于识别心跳版本的 ID
}
/**
@ -375,10 +376,24 @@ class NakamaManager {
_startHeartbeat() {
this._stopHeartbeat();
const currentHeartbeatId = ++this.heartbeatId;
console.log('[Nakama] Starting heartbeat version:', currentHeartbeatId);
this.heartbeatTimer = setInterval(() => {
// 如果此心跳 ID 不再是当前活跃 ID立即停止
if (this.heartbeatId !== currentHeartbeatId) {
console.log('[Nakama] Zombie heartbeat detected and stopped:', currentHeartbeatId);
clearInterval(this.heartbeatTimer);
return;
}
if (this.isConnected) {
this._send({ ping: {} }, 5000).catch(() => {
console.warn('[Nakama] Heartbeat failed');
this._send({ ping: {} }, 5000).catch((err) => {
console.warn('[Nakama] Heartbeat failed:', err.message);
// 如果发送失败且 socketTask 已断开,触发清理
if (!this.socketTask || (err.message && err.message.includes('not connected'))) {
this.disconnect();
}
});
}
}, this.heartbeatInterval);