Search Unity

[BUG] ? NetworkServer.connections not clearing

Discussion in 'Multiplayer' started by Defiled, Jun 2, 2018.

  1. Defiled

    Defiled

    Joined:
    Feb 10, 2014
    Posts:
    43
    Hey guys I ran into an issue with 'NetworkServer.connections', when I launch the host and some one connects it adds +1 correctly, but when they leave it does not remove him from the 'NetworkServer.connections', it just turns it null.. and since it's a read-only list I cannot change that my self, is there a function or command to fix this?

    These are the only things I changed in my networkManager

    Code (CSharp):
    1.     public override void OnServerReady(NetworkConnection conn)
    2.     {
    3.         base.OnServerReady(conn);
    4.         foreach (KeyValuePair<NetworkInstanceId, NetworkIdentity> objects in NetworkServer.objects)
    5.         {
    6.             ObjectPosition targetedObject = NetworkServer.FindLocalObject(objects.Key)?.GetComponent<ObjectPosition>();
    7.             if (targetedObject != null)
    8.             {
    9.                 targetedObject.Target_SyncObject(conn, targetedObject.transform.name, targetedObject.transform.position);
    10.             }
    11.         }
    12.     }
    13.  
    14.     public override void OnServerDisconnect(NetworkConnection conn)
    15.     {
    16.         List<NetworkInstanceId> objects = conn?.clientOwnedObjects?.ToList();
    17.  
    18.         if (objects != null)
    19.         {
    20.             foreach (var obj in objects)
    21.             {
    22.                 GameObject target = NetworkServer.FindLocalObject(obj);
    23.                 if (target != null && conn.playerControllers.Where(x => x.gameObject == target).FirstOrDefault() == null)
    24.                 {
    25.                     target.GetComponent<NetworkIdentity>().RemoveClientAuthority(conn);
    26.                     Debug.Log($"{target.name} is no longer owned");
    27.                 }
    28.             }
    29.         }
    30.         NetworkServer.DestroyPlayersForConnection(conn);
    31.     }
    32.  
    33.     public override void OnStopHost()
    34.     {
    35.         base.OnStopHost();
    36.         ClientScene.DestroyAllClientObjects();
    37.         ClientScene.localPlayers.Clear();
    38.     }
    39.  
    40.     public override void OnStopClient()
    41.     {
    42.         base.OnStopClient();
    43.         ClientScene.DestroyAllClientObjects();
    44.         ClientScene.localPlayers.Clear();
    45.     }
    46.  
    47.     public void Update()
    48.     {
    49.         if (Input.GetButtonDown("R_SELECT"))
    50.         {
    51.                 ClientScene.AddPlayer(client.connection, (short)0);
    52.         }
    53.     }
    Debugging it this way

    Code (CSharp):
    1.     private void OnGUI()
    2.     {
    3.         if (isServer)
    4.         {
    5.             GUI.Label(new Rect(10, 10, 500, 50), $"Connections: {playerCount()} \n");
    6.         }
    7.     }
    8.  
    9.     public int playerCount()
    10.     {
    11.         if (!isServer)
    12.             return 0;
    13.  
    14.         return NetworkServer.connections.Count;
    15.     }
    edit
    Did a test with a new project and just ran the bottom code to debug connecting / disconnecting to and from the server, it still does not reset the value properly.
     
    Last edited: Jun 2, 2018
  2. aabramychev

    aabramychev

    Unity Technologies

    Joined:
    Jul 17, 2012
    Posts:
    574
  3. Sam-DreamsMaker

    Sam-DreamsMaker

    Joined:
    May 2, 2015
    Posts:
    13
    NetworkServer.connections.Count increase over time,
    when a NetworkConnection is closed NetworkServer.connections.Count isn't updated.

    A scenario example:
    Server started = > length = 1
    Alice join the server => length = 2
    Bob join the server => length = 3
    Alice disconnect => length = 3
    Alice join the server => length = 3
    Alice & Bob disconnect => length = 3
    Bob or Alice join the server => length = 3
     
  4. Driiade

    Driiade

    Joined:
    Nov 21, 2017
    Posts:
    80
    This list doesn't clean but connections are set to null in case of disconnection.
    You have to check for null reference in the list.
     
    hickna likes this.
  5. Sam-DreamsMaker

    Sam-DreamsMaker

    Joined:
    May 2, 2015
    Posts:
    13
    It's so clean xD, I have many connected players like ghosts on my server, I have to restart it everyday.
    The cleanness of disconnection is like the management of this list.

    We have to check the list everytime and try to remove associated gameobjects with, if there was a problem during the disconnection. It's really uncommon but it's appearing. It's unstable.
     
    Last edited: Aug 3, 2018