Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

ConnectedClients.Count Doesn't Updated" After OnClientDisconnectCallback

Discussion in 'Netcode for GameObjects' started by afavar, Mar 18, 2023.

  1. afavar


    Jul 17, 2013
    I am trying to detect the connected player count on the server after a client is disconnected.

    If the server disconnects a client with the NetworkManager.DisconnectClient() method, getting the NetworkManager.ConnectedClients.Count value is correct.

    However, if I check this value after
    OnClientDisconnectCallback, when a player gets disconnected, it is still indicating the count before the disconnect.

    Am I missing something or should I use something else to check the remaning player count?
  2. afavar


    Jul 17, 2013
    Hmm, It appears like this is intentional based on this. Please correct me if I am wrong though.

    "This issue is caused by the order of operations.
    The connected client list is not updated on the server until the OnClientDisconnected callback is invoked. This was intentional as a user might still want to gather information from the connected client list about the disconnected client."

    So, instead of using the ConnectedClients.Count, I am sending the disconnected client id to my method and using a local variable.

    Code (CSharp):
    1.     private void CheckConnectedPlayersCount(ulong lastDisconnectedClientId)
    2.     {
    3.         int activeCount = 0;
    5.         foreach (var clientId in NetworkManager.Singleton.ConnectedClientsIds)
    6.         {
    7.             if (clientId != lastDisconnectedClientId)
    8.             {
    9.                 activeCount++;
    10.             }
    11.         }
    12.     }
    However, what boggles my mind is there are places in the docs that checks ConnectedClients.ContainsKey to do something with the client. So, if a player disconnects, will this check be also useless if the ServerRpc is called right after a client gets disconnected?
    Last edited: Mar 18, 2023
  3. cerestorm


    Apr 16, 2020
    I had a similar issue of unregistering a custom message channel inside OnClientDisconnectCallback, the network manager didn't like that. If you look at the source code you can see OnClientDisconnectCallback is called early on in the disconnection process and the client hasn't been removed from the client list yet.
    As a client could disconnect at any point in time the server network manager should be able to handle that silently, at least I've not seen any indication otherwise. The trickier part is how you handle that in your game, I typically have a Client class with a ClientState as a starting point for handling disconnects.
    afavar likes this.
  4. lavagoatGG


    Apr 16, 2022
    Why not
    Code (CSharp):
    2. int activeCount = 0;
    3. if(lastDisconnectedClientId != null)
    4. {
    5.     activeCount = NetworkManager.Singleton.ConnectedClientsIds.Count - 1;
    6. }