This commit is contained in:
2026-10-05 01:02:55 +08:00
parent ec3c947e2a
commit 06e1a12ae2
27 changed files with 3714 additions and 39 deletions
+19 -5
View File
@@ -32,6 +32,18 @@ namespace Assets.MyGame.Scripts.Net
public bool isLoggingOut = false;
/// <summary>每个 socketType 正在运行的重连协程引用,用于主动 StopCoroutine</summary>
private readonly Dictionary<SocketType, Coroutine> reconnectCoroutines = new();
/// <summary>重连状态变化事件(type=哪个 socket;true=开始自动重连 / false=重连结束-成功或取消)。
/// 界面(如游戏面板的 Hl_WaitTip"正在连接服务器"提示)据此显隐;与重连生命周期严格一致。</summary>
public static event Action<SocketType, bool> OnReconnectingChanged;
/// <summary>统一维护重连集合并广播变化:reconnecting 的所有 Add/Remove 入口都走这里,避免漏发事件</summary>
private bool SetReconnecting(SocketType type, bool value)
{
bool changed = value ? reconnecting.Add(type) : reconnecting.Remove(type);
if (changed) OnReconnectingChanged?.Invoke(type, value);
return changed;
}
private float nextHeartbeatTime;
private const float HeartbeatIntervalSeconds = 20f;
#if NewIP
@@ -137,7 +149,7 @@ namespace Assets.MyGame.Scripts.Net
#endif
reconnecting.Remove(type);
SetReconnecting(type, false);
handshakeCompleted.Remove(type);
// SignalR 握手完成前不能发送 Bind、JoinRoom 等 Hub 调用。
web.Send("{\"protocol\":\"json\",\"version\":1}\u001e");
@@ -289,7 +301,7 @@ namespace Assets.MyGame.Scripts.Net
/// </summary>
private void StartReconnect(SocketType type, Action action = null)
{
if (!reconnecting.Add(type))
if (!SetReconnecting(type, true))
return;
var co = StartCoroutine(Reconnect(type, action));
reconnectCoroutines[type] = co;
@@ -307,7 +319,9 @@ namespace Assets.MyGame.Scripts.Net
StopCoroutine(kv.Value);
}
reconnectCoroutines.Clear();
reconnecting.Clear();
// 逐个触发"重连取消",界面等待提示能同步收起
foreach (var type in new List<SocketType>(reconnecting))
SetReconnecting(type, false);
}
private System.Collections.IEnumerator Reconnect(SocketType type, Action action = null)
@@ -320,14 +334,14 @@ namespace Assets.MyGame.Scripts.Net
yield return new WaitForSeconds(3f);
if (manualClosing.Contains(type))
{
reconnecting.Remove(type);
SetReconnecting(type, false);
reconnectCoroutines.Remove(type);
yield break;
}
// ⭐ 登出中或未登录:取消已排队等待的重连
if (isLoggingOut || GameManager.Instance.user == null)
{
reconnecting.Remove(type);
SetReconnecting(type, false);
reconnectCoroutines.Remove(type);
yield break;
}