Search Unity

client/server disconnection

Discussion in 'Netcode for GameObjects' started by motillaones, Nov 30, 2022.

  1. motillaones

    motillaones

    Joined:
    Oct 11, 2018
    Posts:
    18
    Hii, doing disconnection tests I have the problem that when the client disconnects everything is fine using this test code:

    LobbyService.Instance.RemovePlayerAsync(lobbyId, playerId);
    Debug.Log("leaving Lobby");
    yield return new WaitForSeconds(2);
    NetworkManager.Singleton.Shutdown();
    NetworkManager networkManager = GameObject.FindObjectOfType<NetworkManager>();
    Destroy(networkManager.gameObject);
    SceneManager.LoadScene(MainMenu, LoadSceneMode.Single);

    the intention is to leave the lobby and the scene in which it is being played and return to the main scene, and from the client side it works fine, but when I do it from the server side all the players disappear, what solution can I apply?
     
  2. RikuTheFuffs-U

    RikuTheFuffs-U

    Unity Technologies

    Joined:
    Feb 20, 2020
    Posts:
    440
    Hi @motillaones , if you disconnect the server then all the clients will lose the connection to it. One thing you can do is to migrate the host/server before shutting down the network manager.
     
  3. motillaones

    motillaones

    Joined:
    Oct 11, 2018
    Posts:
    18
    Last edited: Nov 30, 2022
  4. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    5,892
    Code (CSharp):
    1. Destroy(networkManager.gameObject);
    Why are you destroying the NetworkManager? It should be persistent.
     
  5. motillaones

    motillaones

    Joined:
    Oct 11, 2018
    Posts:
    18
    Hi @CodeSmile , to avoid duplicating it in the main scene
     
  6. RikuTheFuffs-U

    RikuTheFuffs-U

    Unity Technologies

    Joined:
    Feb 20, 2020
    Posts:
    440
    In the documentation page you linked it says:

    So my assumption is that this is done automatically, as long as you added the Relay integration to your game
     
  7. motillaones

    motillaones

    Joined:
    Oct 11, 2018
    Posts:
    18
    @RikuTheFuffs-U If I understand correctly, the migration of the host in the lobby is done automatically or through LobbyService.Instance.RemovePlayerAsync(lobbyId, playerId);
    Doing tests it seems that it works normally, both automatically and manually. But it seems that the migration of the host in the relay server is something separate, that's what I can't figure out. Avoid kicking other players when the host goes offline. I don't know, I'm quite confused
     
  8. RikuTheFuffs-U

    RikuTheFuffs-U

    Unity Technologies

    Joined:
    Feb 20, 2020
    Posts:
    440
    From what I read in the docs, this can be done only if you catch the disconnecting host "in time". Otherwise the underlying connection between players is destroyed.

    I don't think we have somethign out-of-the box, but on these pages i see some logical steps you can take to implement the host migration yourself. Maybe that helps?

    Relay integration (unity.com)
    Host migration (unity.com) (lobby)
    Host migration (unity.com) (relay)
     
  9. cerestorm

    cerestorm

    Joined:
    Apr 16, 2020
    Posts:
    660
    As far as I know NGO doesn't support host migration, I guess the documentation assumes you're using a multiplayer solution that does.
     
  10. motillaones

    motillaones

    Joined:
    Oct 11, 2018
    Posts:
    18
    Hi @cerestorm, yes, you are right but the host migration is not done with NGO, it is done with the lobby & relay itself.

    As far as your code goes (ignoring NGO), there were a few things that you could adjust that will ensure Host Migration is working between Relay and Lobby:
    1. When your client joins the Lobby, they should update their own Lobby.Player.AllocationId to match the current host and ensure Lobby automatically updates when Relay detects a player disconnect.
    2. During the migration, the new host will need to create a new Relay Allocation and update the values in the Lobby so that other clients can connect and join.
      • Note: If you manually migrate a Lobby host (as in your code), you should still create a new Relay Allocation and update the information.


    In this post they talk about the same thing, but I'm not sure how to do the steps exactly and I think that the one who opened that post neither, https://forum.unity.com/threads/how-to-do-host-migration.1252866/
     
  11. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    5,892
    „to avoid duplicating it in the main scene“

    careful with that! If the NetworkManager gets duplicated when changing scenes there is something wrong with your scripts, most likely something keeps a reference to the NetworkManager. Destroying the NetworkManager is a symptom of an underlying issue that should be fixed, not worked around.

    Typically this duplication is caused by either a Singleton, another DontDestroyOnLoad object or a ScriptableObject. In particular check any scripts where you assign the NetworkManager ref to a field or static variable that isn‘t set back to null when the NetworkManager shuts down (client disconnects).

    Destroying the most recent NetworkManager may work now but it could break in future, or just randomly, who knows. With any code you write or with any NGO update.