[UNET] Handle Client Disconnection

I found a lot of players having similar issues, but none of them really solved my problem.

I have a client running on Android, and if I close the app instantly, I couldn’t find a way to handle the disconnection on the server.

Here is what I tried:
On my custom NetworkManager class:

public override void OnServerDisconnect(NetworkConnection conn)
{
    Debug.Log("OnServerDisconnect");
    base.OnServerDisconnect(conn);
    NetworkServer.DestroyPlayersForConnection(conn);
}
public override void OnClientDisconnect(NetworkConnection conn)
{
    Debug.Log("OnClientDisconnect");
    base.OnClientDisconnect(conn);
}

On my custom NetworkBehavior spawned client class:

void OnPlayerDisconnected(NetworkPlayer player)
{
    Debug.Log("OnPlayerDisconnected");
    Network.RemoveRPCs(player);
    Network.DestroyPlayerObjects(player);
}
void OnDisconnectedFromServer(NetworkDisconnection info)
{
    Debug.Log("OnDisconnectedFromServer");
}
void OnApplicationQuit()
{
    if (Network.connections.Length == 0)
        Debug.Log("No one is connected");
    else
    {
        foreach (NetworkPlayer conn in Network.connections)
        {
            Debug.Log("Disconnecting: " + conn.ipAddress + ":" + conn.port);
            Network.CloseConnection(conn, true);
        }
    }
}

No one single method is called, instead I got this error:

Server client disconnect error:2
UnityEngine.Networking.NetworkIdentity:UNetStaticUpdate()

Does anybody know how to handle that?

Thanks!

Hey,

I struggled a long time with UNET.
After some deep research I found out that you have to add a virtual function to your PlayerPrefab
Implemented as follow for the client callback:

	public virtual void OnClientDisconnect(NetworkConnection conn)
	{
		Debug.Log ("Server is stopped from PlayerPrefab");
	}

In the NetworkManager or NetworkLobbyManager add the same method but make it override and call the base function of the network behaviour. Also add the OnServerdisconnect here:

public override void OnClientDisconnect(NetworkConnection conn)
{
	base.OnClientDisconnect (conn);
	Debug.Log ("Server is stopped from Manager");
}

public override void OnServerDisconnect(NetworkConnection conn) 
{
	base.OnServerDisconnect (conn);
	Debug.Log ("Client is stopped from Manager");
}

Now, When I start two instances and stop the client. The server/Host keeps running and I get my log message ‘Client is stopped’ (bizarre it’s called in the ServerDisconnect ?)

When I do the opposite, Stop the server/Host I get the Log message 'Server is stopped) called from the ClientDisconnect. It seems work in the opposite way, but it works.

Not sure how you can get a client Id to show which user has disconnected?

Hopefully this helps,