Search Unity

Question How to know whether a client has disconnected from the host (to reconnect mid-game: HELP!)

Discussion in 'Netcode for GameObjects' started by GuirieSanchez, Feb 7, 2023.

  1. GuirieSanchez

    GuirieSanchez

    Joined:
    Oct 12, 2021
    Posts:
    452
    In my game, after a client stops running his/her app for a few seconds (maybe they are watching an ad or they have the app paused), the host will disconnect them from the game:

    This is totally fine by me since clients can rejoin whenever they want. The issue is that I want a way for the client to be notified they lost connection so that I can take action (maybe display a message, maybe reconnect them automatically and reload the scene). Otherwise, they'll be on the same scene as the host, but they won't be updated, so it feels like the app crashed or something (when in reality they just lost connection).

    For instance, if they paused the game > then lost connection > and finally unpause the game, if they are inside the same scene as the host, I want to automatically send a rejoin request to the server/host and reload the scene. This method is already implemented for clients who closed the app and rejoin from the main menu, and it works fine. I'm wondering if there's a callback or something I can use to know when the client is not connected so that I can do the same reconnecting thing but in this specific case.

    Any help is highly appreciated.
     
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    5,975
    GuirieSanchez likes this.
  3. GuirieSanchez

    GuirieSanchez

    Joined:
    Oct 12, 2021
    Posts:
    452
    Yes, I tried, but, unfortunately, it doesn't seem to be triggering. When the client comes back, it's got no messages from the ClientDisconnectedCallback.

    The fastest way to test it was on the editor, pause it for some seconds until the host kicks him out, then unpause it and there's nothing showing up on the console, while the client (editor) is not receiving any updates.

    PS: this is wild, I test it like this 4 times, and I got the disconnect message from the ClientDisconnectedCallback only 1 time.
     
  4. GuirieSanchez

    GuirieSanchez

    Joined:
    Oct 12, 2021
    Posts:
    452
    Ok, I think I figured out something. Let's assume, for example, that it takes the host just 10 seconds to kick the client out if his game is paused.

    If the client comes back right after he's been kicked out by the host (as soon as the host receives the "Couldn't add payload of size 115 to reliable send queue. Closing connection 1 as reliability guarantees can't be maintained."), then the client does not receive the message from the ClientDisconnectedCallback yet but he is completely frozen (as he's not receiving any updates from the host). It only gets the message after, say, 25-30 seconds have passed, which is bad as it is a big enough amount of time not to be able to play.
     
  5. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    5,975
    I think NetworkManager has some property that tells you whether it's in "shutdown" state (or not initialized). Or if it doesn't maybe you can derive it from a property indirectly, such as a list being empty or a counter or flag getting reset.
     
  6. GuirieSanchez

    GuirieSanchez

    Joined:
    Oct 12, 2021
    Posts:
    452
    I have no idea of what this might be :(
     
  7. qngnht

    qngnht

    Joined:
    Jan 24, 2022
    Posts:
    24