fix: improve minigame reconnect flow

This commit is contained in:
ud18010
2026-07-29 19:30:50 +08:00
parent 6a5e534d00
commit 42c3874d82
21 changed files with 979 additions and 52 deletions
@@ -22,6 +22,7 @@ namespace XGame.MiniGame
private GameClientCtx _ctx;
private MiniGameAssetLoader _assets;
private RoomPlayerService _players;
private readonly List<NetMessage> _queuedInitialMessages = new List<NetMessage>();
private bool _running;
private bool _waitingForResultConfirm;
private string _gameId;
@@ -38,10 +39,22 @@ namespace XGame.MiniGame
public void EnterGame(string cdnBase, string gameId, int version, XWebSocket sock, bool sendMatchRequest)
{
_gameId = gameId; _version = version;
_queuedInitialMessages.Clear();
// FIX: XWCoroutine.StartCo is a static method (not Instance.StartCo)
XWCoroutine.StartCo(CoEnter(cdnBase, gameId, version, sock, sendMatchRequest));
}
public void QueueInitialMessage(NetMessage message)
{
if (_client != null)
{
SafeCall(() => _client.OnNetMessage(message), "OnNetMessage");
return;
}
_queuedInitialMessages.Add(message);
}
// 大厅链路:MatchFound 由大厅通道消费(sendMatchRequest=false 时宿主收不到),
// 由外部把房间信息注入,否则结算弹框无法判断胜负(_selfPlayerId 恒 0)。
public void SetMatchInfo(string roomId, int selfPlayerId)
@@ -122,6 +135,7 @@ namespace XGame.MiniGame
try
{
_client.OnEnter(_ctx);
FlushQueuedInitialMessages();
}
catch (Exception e)
{
@@ -165,6 +179,7 @@ namespace XGame.MiniGame
public void ExitGame()
{
if (!_running && _client == null) return;
_queuedInitialMessages.Clear();
DestroyResultDialog();
_waitingForResultConfirm = false;
_running = false;
@@ -183,6 +198,7 @@ namespace XGame.MiniGame
private void FailEnterAndExit(string reason)
{
_queuedInitialMessages.Clear();
DestroyResultDialog();
_waitingForResultConfirm = false;
_running = false;
@@ -443,6 +459,18 @@ namespace XGame.MiniGame
return button;
}
private void FlushQueuedInitialMessages()
{
if (_client == null || _queuedInitialMessages.Count == 0) return;
NetMessage[] messages = _queuedInitialMessages.ToArray();
_queuedInitialMessages.Clear();
for (int i = 0; i < messages.Length; i++)
{
NetMessage message = messages[i];
SafeCall(() => _client.OnNetMessage(message), "OnNetMessage");
}
}
private static void SafeCall(Action a, string where)
{
try { a(); }
@@ -1,5 +1,6 @@
using System.Collections.Generic;
using UnityEngine;
using XGame;
namespace XGame.MiniGame
{
@@ -17,6 +18,19 @@ namespace XGame.MiniGame
public bool IsSuspended { get; private set; }
public static bool HasSuspendedLobby()
{
MiniGameStageIsolation[] isolations = FindObjectsOfType<MiniGameStageIsolation>(true);
for (int i = 0; i < isolations.Length; i++)
{
if (isolations[i] != null && isolations[i].IsSuspended)
{
return true;
}
}
return false;
}
public void SuspendLobbyForMiniGame()
{
if (IsSuspended)
@@ -77,6 +91,7 @@ namespace XGame.MiniGame
_behaviourStates.Clear();
_canvasGroupStates.Clear();
CaptureGameObjects(LobbyUiRoots);
CaptureCharacterSwitchUiRoots();
CaptureGameObjects(LobbySceneRoots);
CaptureBehaviours(LobbyInputBehaviours);
CaptureBehaviours(LobbyTickBehaviours);
@@ -86,6 +101,7 @@ namespace XGame.MiniGame
private void ApplySuspendedState()
{
SetGameObjectsActive(LobbyUiRoots, false);
SetCharacterSwitchUiRootsActive(false);
SetGameObjectsActive(LobbySceneRoots, false);
SetBehavioursEnabled(LobbyInputBehaviours, false);
SetBehavioursEnabled(LobbyTickBehaviours, false);
@@ -105,6 +121,19 @@ namespace XGame.MiniGame
}
}
private void CaptureCharacterSwitchUiRoots()
{
CharacterSwitchController[] controllers = FindObjectsOfType<CharacterSwitchController>(true);
for (int i = 0; i < controllers.Length; i++)
{
GameObject target = controllers[i] != null ? controllers[i].StageIsolationUiRoot : null;
if (target != null)
{
_gameObjectStates.Add(new GameObjectState(target, target.activeSelf));
}
}
}
private void CaptureBehaviours(Behaviour[] targets)
{
if (targets == null) return;
@@ -143,6 +172,19 @@ namespace XGame.MiniGame
}
}
private static void SetCharacterSwitchUiRootsActive(bool active)
{
CharacterSwitchController[] controllers = FindObjectsOfType<CharacterSwitchController>(true);
for (int i = 0; i < controllers.Length; i++)
{
GameObject target = controllers[i] != null ? controllers[i].StageIsolationUiRoot : null;
if (target != null)
{
target.SetActive(active);
}
}
}
private static void SetBehavioursEnabled(Behaviour[] targets, bool enabled)
{
if (targets == null) return;