Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question How to determine if the server is shutting down on client side without RPC?

Discussion in 'Netcode for GameObjects' started by CodeSmile, Oct 9, 2022.

  1. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    6,467
    I have a simple setup with host and client(s), and they all listen to OnClientDisconnected.

    I noticed that when calling NetworkManager.Singleton.Shutdown() on the server side, the clients have only one means of determining the server shutting down (besides writing RPC calls), as follows:

    • client subscribes to NetworkManager's OnClientDisconnected
    • client checks whether the disconnecting clientId is that of the server

    Example code snippet that works to confirm on client-side that the host has shut down:
    Code (CSharp):
    1. private void OnClientDisconnected(ulong clientId)
    2. {
    3.     if (clientId == NetworkManager.ServerClientId)
    4.         Debug.LogWarning("server shutting down");
    5. }
    Client NetworkManager's status is still "ConnectedClient == true" and "ShutdownInProgress == false" when the host disconnect callback runs.

    I just wonder whether that is the proper way of determining on the client side that the host (server) is shutting down?
    (again: besides RPC calls)
     
    BSimonSweet likes this.
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    6,467
    FWIW using this "detect server shutdown" approach I was able to establish a Host/Join connect/disconnect cycle where any running build can host, any client can repeatedly join and disconnect, host can shut down at any time and another (still running) build can host and the others join. All within the same scene.
     
  3. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    6,467
    Note: OnClientDisconnected always receives 0 as the clientId when the server shuts down or calls DisconnectClient().
    0 equals NetworkManager.ServerClientId in my test cases.

    I would have expected this to match the local client's clientId. So if clientId matches server, client knows he was either kicked or server shutdown or (haven't checked this) connection was lost on either end. Can't remember this being mentioned in the docs though.
     
    Last edited: Oct 9, 2022