Search Unity

NetworkManager reconnection doesn't work

Discussion in 'Multiplayer' started by shoo, Feb 22, 2017.

  1. shoo

    shoo

    Joined:
    Nov 19, 2012
    Posts:
    67
    Hello! I am trying to implement independent network connection.
    By "independent" I mean, server and client will reconnect each other and sync state after connection was lost.

    So, for Server my NetworkManager works this way:
    Code (CSharp):
    1.     IEnumerator HandleServer()
    2.     {
    3.         while (true)
    4.         {
    5.             bool isConnected = false;
    6.             bool isError = false;
    7.  
    8.             while (!isConnected)
    9.             {
    10.                 if (isError)
    11.                 {
    12.                     Debug.Log("Waiting for 5 seconds becouse error!");
    13.                     yield return new WaitForSeconds(5f);
    14.                 }
    15.  
    16.                 try
    17.                 {
    18.                     discovery.Initialize();
    19.                     discovery.StartAsServer();
    20.                     manager.StartServer();
    21.  
    22.                     isConnected = true;
    23.                 }
    24.                 catch (Exception e)
    25.                 {
    26.                     // also I need to show it in my interface
    27.                     isError = true;
    28.                     LogMan.manager.AddLine("Error connecting to network! Trying again in 5 seconds." + e.ToString());
    29.                 }
    30.             }
    31.  
    32.             // wait while connection available
    33.             while (manager.isNetworkActive)
    34.                 yield return null;
    35.  
    36.             discovery.StopBroadcast();
    37.             manager.StopServer();
    38.         }
    39.     }
    And for Client my NetworkManager works this way:
    Code (CSharp):
    1.     IEnumerator HandleClient()
    2.     {
    3.         while (true)
    4.         {
    5.             discovery.Initialize();
    6.  
    7.             string serverAddress = null;
    8.             discovery.ReceiveBroadcast = (string fromAddress) => serverAddress = fromAddress;
    9.  
    10.             discovery.StartAsClient();
    11.  
    12.             // waiting while discovery will receive broadcast
    13.             while (serverAddress == null)
    14.                 yield return null;
    15.  
    16.             discovery.StopBroadcast();
    17.  
    18.             manager.networkAddress = serverAddress;
    19.  
    20.             manager.StartClient();
    21.  
    22.             while (manager.isNetworkActive)
    23.                 yield return null;
    24.  
    25.             manager.StopClient();
    26.         }
    27.     }
    First time it always connects properly. But after restarting Server, Client sometimes connects to server and sometimes not. I tryed to point out why thats happens, but I really have no idea.

    It looks like sometimes at reconnection, server handle OnServerConnect event and then handle OnServerDisconnect, missing OnServerAddPlayer event. After that, connection on client becomes totally broken. When I restart Client, it works again.

    Seem I need somehow reinitialize my client connection before reconnecting? But how can I do this?
     
    Last edited: Feb 22, 2017
  2. shoo

    shoo

    Joined:
    Nov 19, 2012
    Posts:
    67
    I figured out, NetworkManager sometimes doesn't call "public override void OnClientDisconnect()", so connection stays in broken state.
    But I have no idea how to fix it, maybe it is Unity 5.4 bug :/
     
    Last edited: Feb 22, 2017
  3. White8eard

    White8eard

    Joined:
    Aug 4, 2014
    Posts:
    61
    i read about reconnection system is broken few months back
     
    shoo likes this.
  4. shoo

    shoo

    Joined:
    Nov 19, 2012
    Posts:
    67
    I got rid of discovery component and using ipv4 static ip now. Seem's it works better.
    Also, NetworkManager.StartClient() always calls OnClientDisconnect() event(even if wasn't connected), so that was a place where my client was getting stuck.

    But I still getting
    Instance not found when handling Command message [netId=2]
    UnityEngine.Networking.NetworkIdentity:UNetStaticUpdate()
    On the server sometimes.
     
    Last edited: Feb 23, 2017