- Home /
[UNET] How to handle disconnection?
I'm currently working on a multiplayer card game, and I'm trying to handle player disconnections, and finding frustratingly little in the docs about what cleanup needs to be done when stopping the server, leaving the lobby, leaving a match, destroying matches and ensuring that players can't join a match with 0 players already in it.
public override void OnLobbyServerDisconnect(NetworkConnection conn)
{
if (isHost)
{
Debug.LogError("Destroying Host!");
matchMaker.DestroyMatch((UnityEngine.Networking.Types.NetworkID)MatchNetworkID, OnDestroyMatch);
}
else
{
DropConnectionRequest dropReq = new DropConnectionRequest();
dropReq.networkId = (UnityEngine.Networking.Types.NetworkID)MatchNetworkID;
dropReq.nodeId = (UnityEngine.Networking.Types.NodeID)MatchNodeID;
matchMaker.DropConnection(dropReq, OnConnectionDrop);
}
base.OnLobbyServerDisconnect(conn);
}
public void DisconnectFromNetwork()
{
numUsers = 0;
if (isHost)
{
StopHost();
StopMatchMaker();
}
else
{
StopClient();
}
}
isHost is a boolean set when the server is started on the host.
What steps need to be taken to ensure a clean disconnect when a player clicks the disconnect button?
sorry but i didn't FULL understand you question do you mean how to disconnect a client when he hit a button or what ?
Answer by RonnyKibet · Aug 08, 2016 at 01:45 PM
NetworkManager.singleton.StopHost();
I use that to disconnect the host and clients at the same time by attaching to a button. One thing to note is that if the host that started the match clicks the button, all other clients disconnects, but when a clients clicks the button, only the client that clicked is disconnected and the host keeps running.
StopHost is freezing the editor for some reason...