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

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:

private void OnClientDisconnected(ulong clientId)
{
    if (clientId == NetworkManager.ServerClientId)
        Debug.LogWarning("server shutting down");
}

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)

2 Likes

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.

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.