匹配自动重连

- 修复第一次连接时ClientID为0导致匹配失败的问题
- 在GameClientMatchmaker中添加ClientID检测和自动重连逻辑
- 当检测到ClientID为0时,等待0.5秒后自动断开并重新连接
- 优化MatchmakingPanel显示逻辑,根据ClientID状态显示正确的连接状态
This commit is contained in:
xianyi
2025-10-16 11:43:27 +08:00
parent 64d01f317b
commit 12a19ba1a9
2 changed files with 57 additions and 5 deletions

View File

@@ -25,6 +25,9 @@ namespace TcgEngine.Client
private int matchmaking_players;
private UnityAction<bool> connect_callback;
private float clientid_check_timer = 0f;
private bool reconnecting = false;
private static GameClientMatchmaker _instance;
void Awake()
@@ -62,8 +65,41 @@ namespace TcgEngine.Client
timer += Time.deltaTime;
match_timer += Time.deltaTime;
ulong client_id = TcgNetwork.Get().ClientID;
//检测ClientID为0的情况连接未完全建立
if (IsConnected() && client_id == 0 && !reconnecting)
{
clientid_check_timer += Time.deltaTime;
//如果超过3秒ClientID仍为0断开并重连
if (clientid_check_timer > 0.5f)
{
Debug.Log("ClientID is 0 after 3 seconds, reconnecting...");
reconnecting = true;
//保存匹配信息
string saved_group = matchmaking_group;
int saved_players = matchmaking_players;
//断开连接
Disconnect();
//延迟后重新开始匹配
StartCoroutine(ReconnectAfterDelay(saved_group, saved_players));
clientid_check_timer = 0f;
}
}
else if (IsConnected() && client_id != 0)
{
//ClientID正常重置计时器
clientid_check_timer = 0f;
reconnecting = false;
}
//Send periodic request
if (IsConnected() && TcgNetwork.Get().ClientID != 0 && timer > 2f)
if (IsConnected() && client_id != 0 && timer > 2f)
{
timer = 0f;
SendMatchRequest(true, matchmaking_group, matchmaking_players);
@@ -77,6 +113,15 @@ namespace TcgEngine.Client
}
}
private IEnumerator ReconnectAfterDelay(string group, int players)
{
yield return new WaitForSeconds(0.5f);
Debug.Log($"Reconnecting with group: {group}, players: {players}");
StartMatchmaking(group, players);
reconnecting = false;
}
public void StartMatchmaking(string group, int nb_players)
{
if (matchmaking)