匹配自动重连
- 修复第一次连接时ClientID为0导致匹配失败的问题 - 在GameClientMatchmaker中添加ClientID检测和自动重连逻辑 - 当检测到ClientID为0时,等待0.5秒后自动断开并重新连接 - 优化MatchmakingPanel显示逻辑,根据ClientID状态显示正确的连接状态
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
using TcgEngine.Client;
|
||||
using TcgEngine;
|
||||
@@ -34,18 +35,24 @@ namespace TcgEngine.UI
|
||||
{
|
||||
base.Update();
|
||||
|
||||
if (GameClientMatchmaker.Get().IsConnected())
|
||||
text.text = "Finding Opponent...";
|
||||
GameClientMatchmaker matchmaker = GameClientMatchmaker.Get();
|
||||
bool is_connected = matchmaker.IsConnected();
|
||||
ulong client_id = TcgNetwork.Get().ClientID;
|
||||
|
||||
// 显示连接状态
|
||||
if (is_connected && client_id != 0)
|
||||
text.text = "正在寻找对手...";
|
||||
else
|
||||
text.text = "Connecting to server...";
|
||||
text.text = "正在连接到服务器...";
|
||||
|
||||
code_txt.text = "";
|
||||
|
||||
string group = GameClientMatchmaker.Get().GetGroup();
|
||||
string group = matchmaker.GetGroup();
|
||||
if (group != null && group.StartsWith("code_"))
|
||||
code_txt.text = group.Replace("code_", "");
|
||||
}
|
||||
|
||||
|
||||
public void SetCount(int players)
|
||||
{
|
||||
if (players_txt != null)
|
||||
|
||||
Reference in New Issue
Block a user