I'm using Lidgren to connect a unity game to a gameserver. everything works fine. the only problem is when the client disconnects from the server (by loss of internet connection or even manually) it cannot connect again. I have to close and reopen the game to be able to connect again. I have tried this outside the unity too and got the same results:
connection method is like this:
public IEnumerator Connect()
{
m_Client.Start();
var hail = m_Client.CreateMessage();
AuthRequest id = new AuthRequest()
{
Name = PlayerSettings.Name,
};
hail.Write(new Packet(PacketType.AuthRequest, id).SerializePacket());
m_Client.Connect(k_ServerAddress, k_Port, hail);
Debug.Log("Connecting " + id.Name + " to " + k_ServerAddress + ":" + k_Port);
NetConnectionStatus status = m_Client.ConnectionStatus;
while (m_Client.ConnectionStatus != NetConnectionStatus.Disconnected)
{
if (m_Client.ConnectionStatus != status)
{
status = m_Client.ConnectionStatus;
Debug.Log("Status: " + status);
}
yield return null;
}
}
If I use this code:
LidgrenClient m_Client = new LidgrenClient();
m_Client.Connect();
the client gets connected, but when the client gets disconnected from the server the update method below just logs Trying to Reconnect every 5 minutes but actually does nothing:
void Update()
{
if (!waiting && !m_Client.Connected)
{
Debug.Log("Trying to Reconnect");
StartCoroutine(TryReconnect());
}
m_Client.ReadMessages();
}
IEnumerator TryReconnect()
{
waiting = true;
m_Client.Connect();
yield return new WaitForSeconds(5f);
waiting = false;
}