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.
  2. Join us on Thursday, June 8, for a Q&A with Unity's Content Pipeline group here on the forum, and on the Unity Discord, and discuss topics around Content Build, Import Workflows, Asset Database, and Addressables!
    Dismiss Notice

Question How to detect when a disconnection has happened?

Discussion in 'NetCode for ECS' started by EmmaPrats, Jan 17, 2023.

  1. EmmaPrats


    Apr 19, 2019
    1. Detecting disconnection from Client in the Server

    Is there a callback I can listen to?

    I know that when a client disconnects, its "NetworkConnection" entity (the one that has a
    ) is destroyed in the Server World. Clearly, Netcode for Entities does detect when a client disconnects. How can my server's logic detect that too?

    I am aware that there is a "proper" way to disconnect with NetworkStreamRequestDisconnect. I don't want to rely on that, if the client crashes or the player kills the game's process I want to know that they disconnected.

    2. Detecting disconnection from Server in the Client

    What is the appropriate way?

    Thank you for your help!
  2. NikiWalker


    Unity Technologies

    May 18, 2021
    Hey Emma,

    Yes, you can track both connections and disconnections via the optional `ConnectionState` `ICleanupComponentData`.

    "PlayerList" is a new sample showing this feature off, found here:
    This sample uses RPCs to create a "feed" of players who have connected/disconnected.
    One other approach (which isn't demonstrated here) is to implement this behaviour via a static-optimized ghost (with an IBufferElementData containing all connected players).

    For this implementation, take a look at the server logic here:

    ConnectionState is added on like 67, where we have a query of all new `NetworkStreamConnection` entities, where we add the optional component.

    New joiners are handled in the `HandleNewJoinersJob` on line 101. We wait for a client to send an RPC containing their `Username`. Thus, each client must opt-into being seen in the player list. Obviously, for your own game, you can source usernames however you like. E.g. The GameServer could communicate with a backend instead.

    For disconnecting players (regardless of disconnect reason): The `NotifyPlayersOfDisconnectsJob` job on line 184 will show you the query, and how it's used to detect disconnects.

    The `ClientPlayerListSystem`:
    • Registers a username with the "PlayerList" sub-system.
    • Receives these "PlayerList" change events.
    • Uses a cache of this data to draw the Player List, and all NOTIFICATIONS (via IMGUI).

    You can also use new controls in the "Multiplayer > Window: PlayMode Tools" to test various forms of disconnect. The next few pre-releases will contain improvements to this window.
    Last edited: Jan 17, 2023
    KindredKara, PolarTron and EmmaPrats like this.