73 lines
1.6 KiB
C#
73 lines
1.6 KiB
C#
using System.Collections;
|
|
using UnityEngine;
|
|
using Mirror;
|
|
|
|
public class NetManager : NetworkManager
|
|
{
|
|
[SerializeField] float reconnectIntervalSeconds = 15f;
|
|
Coroutine reconnectCoroutine;
|
|
|
|
public override void OnStopServer()
|
|
{
|
|
GameManager.ReportDedicatedMatchRoomClosed();
|
|
base.OnStopServer();
|
|
}
|
|
|
|
public override void OnStopClient()
|
|
{
|
|
base.OnStopClient();
|
|
TryStartReconnectLoop();
|
|
}
|
|
|
|
public override void OnClientConnect()
|
|
{
|
|
base.OnClientConnect();
|
|
StopReconnectLoop();
|
|
}
|
|
|
|
void TryStartReconnectLoop()
|
|
{
|
|
if (!ShouldReconnect() || reconnectCoroutine != null)
|
|
return;
|
|
|
|
reconnectCoroutine = StartCoroutine(CoroutineReconnectLoop());
|
|
}
|
|
|
|
void StopReconnectLoop()
|
|
{
|
|
if (reconnectCoroutine == null)
|
|
return;
|
|
|
|
StopCoroutine(reconnectCoroutine);
|
|
reconnectCoroutine = null;
|
|
}
|
|
|
|
bool ShouldReconnect()
|
|
{
|
|
if (NetworkServer.active)
|
|
return false;
|
|
if (NetworkClient.isConnected)
|
|
return false;
|
|
if (GameManager.instance != null && GameManager.instance.isGameEnded)
|
|
return false;
|
|
|
|
return true;
|
|
}
|
|
|
|
IEnumerator CoroutineReconnectLoop()
|
|
{
|
|
while (ShouldReconnect())
|
|
{
|
|
if (!NetworkClient.active)
|
|
{
|
|
Debug.Log("Client disconnected. Reconnecting...");
|
|
StartClient();
|
|
}
|
|
|
|
yield return new WaitForSeconds(reconnectIntervalSeconds);
|
|
}
|
|
|
|
reconnectCoroutine = null;
|
|
}
|
|
}
|