- Home /
[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!
I found a discussion related to this question that explain that it is a bug: https://forum.unity3d.com/threads/any-way-to-tap-into-ondisconnecterror.431385/
The version 5.4.3 fix the bug, but the GVRDayDream Technical Preview does not have a 5.4.3 yet, only 5.4.2.
Thanks!
Answer by Bertlapp · May 21, 2018 at 01:13 PM
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,
Hi, I have the same problem as antoniohof. The OnServerDisconnect does not call what so ever and except when I get timeout error messages is the only time OnserverDisconnect is called.
These are my functions on network manager class
public override void OnServerDisconnect(NetworkConnection conn) { base.OnServerDisconnect(conn); NetworkServer.DestroyPlayersForConnection(conn); }
public override void OnClientDisconnect(NetworkConnection conn) { base.OnClientDisconnect(conn); Debug.Log("Client Disconnected and player removed"); }
should I place these functions in other classes or do I need to implement other functions for these to work. Please could you explain a little in detail.