Search Unity

Question ClientDisconnection

Discussion in 'Netcode for GameObjects' started by liambilly, Feb 27, 2023.

  1. liambilly

    liambilly

    Joined:
    May 13, 2022
    Posts:
    154
    hello guys, im trying to figure ouhow a client can properly disconnect, currently when idisconnect a client and reconnect them back therirOwnerClientID is different evn when no other player has joined since they disconnected, sometimes i get a warning saying NetworkBehaviour is being destroyed while NetworkObject is still spawned! (could break state synchronization) and other times its an error something like saying key not found in dictionary.
     
    Last edited: Feb 28, 2023
  2. liambilly

    liambilly

    Joined:
    May 13, 2022
    Posts:
    154
    currently this is how i handle disconnection, after a client presses a button for networkmanager.singleton.shutdown()
    Code (CSharp):
    1. NetworkManager.Singleton.OnClientDisconnectCallback += (clientId) =>
    2.         {
    3.             if (!IsServer) return;
    4.             playerCount.Value = (byte)NetworkManager.Singleton.ConnectedClients.Count;
    5.             NetworkManager.Singleton.DisconnectClient(clientId);
    6.         };
     
  3. cerestorm

    cerestorm

    Joined:
    Apr 16, 2020
    Posts:
    666
    I've not looked at the underlying code but the clientId just seems to increment, even if connection approval is refused.

    After calling Shutdown on the client you can poll on ShutdownInProgress to make sure everything network related has been cleaned up. An OnShutdownComplete callback would be really useful.

    You shouldn't need to call NetworkManager.Singleton.DisconnectClient(clientId) on the server in OnClientDisconnectCallback just use that callback for your own housekeeping.
     
  4. liambilly

    liambilly

    Joined:
    May 13, 2022
    Posts:
    154
    i cant find OnShutdownComplete callback ,how do you implement clean up
     
  5. cerestorm

    cerestorm

    Joined:
    Apr 16, 2020
    Posts:
    666
    There isn't an OnShutdownComplete callback unfortunately, as I said it would be useful to have.

    I mention clean up relating to the network side, if you poll until ShutdownInProgress is false then it will be safe to reconnect. It's something you'll have to experiment with, due to the lack of a shutdown callback what I'm doing at the moment is getting the server to disconnect the client.
     
  6. liambilly

    liambilly

    Joined:
    May 13, 2022
    Posts:
    154
    i have seen shutdown() takes a parameter
    bool discardMessageQueue that defaulted as false; would setting it to true also help?
     
  7. cerestorm

    cerestorm

    Joined:
    Apr 16, 2020
    Posts:
    666
    Going by the description I would leave it as false.
     
  8. liambilly

    liambilly

    Joined:
    May 13, 2022
    Posts:
    154
    so currently ShutdownInProgress is the only way to avoid the game crashing when clients are disconnecting and connecting? perhaps then i can disable the starthosthost button when its true;
     
  9. liambilly

    liambilly

    Joined:
    May 13, 2022
    Posts:
    154
    and even with this issue of clientId that keeps increamenting even if the player is no longer connected. so that also a bug? i should probably open an issue on this two factors
     
  10. cerestorm

    cerestorm

    Joined:
    Apr 16, 2020
    Posts:
    666
    Give ShutdownInProgress a whirl, it won't hurt to try it. I did post on Github about an OnShutdownComplete callback and was going to open a request but noticed one had been made already, no response on it though.

    I don't see the incrementing clientId as a bug, you can always implement your own way of checking for reconnecting players with some form of unique id.
     
  11. liambilly

    liambilly

    Joined:
    May 13, 2022
    Posts:
    154
    yes im looking into how to properly implement ShutdownInProgress, will tell you if it will work
    as for that incrementing id issue, the problem is i have a shooter game and when a player spawns i assign them a layer with their id to ensure raycast can hit other players. layers go upto 31 minus the defaultones, if a player keeps disconnecting and reconnecting the layers for others will became less
     
  12. cerestorm

    cerestorm

    Joined:
    Apr 16, 2020
    Posts:
    666
    You could look into having a queue for the layers, pull them out on connect and return on disconnect.
     
  13. liambilly

    liambilly

    Joined:
    May 13, 2022
    Posts:
    154
    interesting ill look into it so instead of using thier ids i used their index in the queue