Search Unity

Question Detecting client dropout during testing on 1 pc

Discussion in 'Multiplayer' started by BIT-64, Mar 25, 2024.

  1. BIT-64

    BIT-64

    Joined:
    Apr 29, 2017
    Posts:
    43
    Hello,

    I am trying to test a multiplayer game and have ran into an interesting problem. I am testing on 1 device and have the multiplayer play mode create 3 additional instances of the game for testing.

    When a client connects to the host this variable increments by 1, 1 extra player, int connectionCount = NetworkManager.Singleton.ConnectedClientsList.Count;, however the problem I am having is when I close the other instances they do not disconnect, I waited several minutes to see if it was a timeout thing but it's not, I can't get an updated player count once the player leaves.

    Does anyone have any idea how to work around this?

    Code (CSharp):
    1. using Unity.Netcode;
    2. using UnityEngine;
    3.  
    4. public class NetworkSynchronizer : NetworkBehaviour
    5. {
    6.     private int connectedClientsCount;
    7.     // Start is called before the first frame update
    8.     void Start()
    9.     {
    10.         this.name = "obj_NetworkSynchronizer";
    11.     }
    12.  
    13.     // Update is called once per frame
    14.     void Update()
    15.     {
    16.         //testing
    17.         int connectionCount = NetworkManager.Singleton.ConnectedClientsList.Count;
    18.         Debug.Log("Number of connected clients, including host: " + connectionCount);
    19.         Debug.Log(connectedClientsCount);
    20.     }
    21.  
    22.     private void OnEnable()
    23.     {
    24.         NetworkManager.Singleton.OnClientConnectedCallback += OnClientConnected;
    25.         NetworkManager.Singleton.OnClientDisconnectCallback += OnClientDisconnected;
    26.     }
    27.  
    28.     private void OnDisable()
    29.     {
    30.         NetworkManager.Singleton.OnClientConnectedCallback -= OnClientConnected;
    31.         NetworkManager.Singleton.OnClientDisconnectCallback -= OnClientDisconnected;
    32.     }
    33.  
    34.     private void OnClientConnected(ulong clientId)
    35.     {
    36.         if (NetworkManager.Singleton.IsServer)
    37.         {
    38.             connectedClientsCount++;
    39.             // Handle a new connection here
    40.             Debug.Log($"Client {clientId} has connected");
    41.             int connectionCount = NetworkManager.Singleton.ConnectedClientsList.Count;
    42.             Debug.Log("Number of connected clients, including host: " + connectionCount);
    43.         }
    44.        
    45.     }
    46.  
    47.     private void OnClientDisconnected(ulong clientId)
    48.     {
    49.         if (NetworkManager.Singleton.IsServer)
    50.         {
    51.             connectedClientsCount--;
    52.             // Handle a disconnection here
    53.             Debug.Log($"Client {clientId} has disconnected");
    54.             int connectionCount = NetworkManager.Singleton.ConnectedClientsList.Count;
    55.             Debug.Log("Number of connected clients, including host: " + connectionCount);
    56.         }
    57.     }
    58. }