fix: improve minigame reconnect flow
This commit is contained in:
@@ -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(); }
|
||||
|
||||
Reference in New Issue
Block a user