Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How to Disconnect Clients properly?

Discussion in 'Multiplayer' started by The_BenEvans, Mar 23, 2016.

  1. The_BenEvans

    The_BenEvans

    Joined:
    Jul 31, 2012
    Posts:
    139
    I've been getting to grips with uNet, using the Lobby Example as a base. The one thing I can't work out or find suitable information on is on how to disconnect Clients correctly.

    The game itself is a 1 vs 1, so one person hosts and the other player is a Client. I've added a "disconnect" button and using this code when the button is pressed:
    Code (CSharp):
    1. NetworkManager.singleton.client.Disconnect();
    This works as intended for the Host and returns them to the Offline Scene. I can't get it to work for the Client, it just stops messages to/from the server, but fails to change the scene or run "OnClientDisconnect" on the LobbyManager script which it has been doing for the Host.

    Is this a bug or am I doing things wrongly?
     
  2. gamevanilla

    gamevanilla

    Joined:
    Dec 28, 2015
    Posts:
    968
  3. Christoph_DonopInteractive

    Christoph_DonopInteractive

    Joined:
    Sep 24, 2013
    Posts:
    3
    Hi, it seems you use the NetworkManager, why not use:

    Code (CSharp):
    1. NetworkManager.singleton.StopClient();
    For the Client and

    Code (CSharp):
    1. NetworkManager.singleton.StopHost();
    For your Host Player
     
    Azurne likes this.
  4. The_BenEvans

    The_BenEvans

    Joined:
    Jul 31, 2012
    Posts:
    139
    Thanks for the quick replies, I'll try that out
     
  5. The_BenEvans

    The_BenEvans

    Joined:
    Jul 31, 2012
    Posts:
    139
    It works! And works with the OnStopClient() override, thanks!
     
  6. Kurius

    Kurius

    Joined:
    Sep 29, 2013
    Posts:
    412
    I know this is an old thread but it's a top Google search result. What finally solved it for me was to have the client locate the host player GameObject, then call Command and ClientRpc methods via the Host in order to simulate the Host leaving the game and thus ending the game for everybody. This was the only full proof method for the Client to be able to successfully leave the current networked multiplayer game (but not quit the app), then join a new networked game. I know this is not an ideal solution, but it works 100% of the time.
     
  7. xbiris122

    xbiris122

    Joined:
    Oct 30, 2018
    Posts:
    1
    What do you mean by "simulate the Host leaving the game and thus ending the game for everybody"?
    Wouldn't that actually disconnect everyone?
     
  8. Punfish

    Punfish

    Joined:
    Dec 7, 2014
    Posts:
    389
    This thread was started in 2016. UNet doesn't exist anymore :p