Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by antoniohof · Dec 08, 2016 at 06:44 AM · androidnetworkingnetworkconnectiondisconnect

[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!

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image antoniohof · Dec 07, 2016 at 12:48 PM 0
Share

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!

avatar image Seathan · Jun 29, 2020 at 06:24 PM 0
Share

Thanks for this question !

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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,

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image satish_dumbu · May 24, 2018 at 07:37 PM 0
Share

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.

avatar image Seathan · Jun 29, 2020 at 06:24 PM 0
Share

Thanks for this answer !!!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

UNet NetworkServer max connections 4 Answers

Android game crashes when attempting to start wifi hotspots 0 Answers

Insert Wi-Fi settings in Unity APP 0 Answers

Question about Network.Destroy 0 Answers

Some questions about unity app's tcp connection state in Android sleep mode 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges