client/server disconnection

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();
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?

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.

Thanks for reply @PaoloAbela , EDIT; I was looking on lobby, when actually the host migration is done in relay Host migration
How do I move the host role to another player?

Destroy(networkManager.gameObject);

Why are you destroying the NetworkManager? It should be persistent.

Hi @CodeSmile , to avoid duplicating it in the main scene

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

@PaoloAbela 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

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)

As far as I know NGO doesn’t support host migration, I guess the documentation assumes you’re using a multiplayer solution that does.

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:

  • 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.

  • 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, How to do host migration?

„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.