Search Unity

UNET HLAPI lobby: I keep rejoining my cancelled game. How to disconnect a server properly?

Discussion in 'UNet' started by MrLucid72, Dec 15, 2016.

  1. MrLucid72

    MrLucid72

    Joined:
    Jan 12, 2016
    Posts:
    988
    The easiest way to disconnect a client without errors seems to be:

    myNetworkLobbyManager.StopHost(); // Not sure if this is the best way, but hey

    However, disconnecting a server using StopHost() works, BUT for the next 30 seconds, clients can still join. In fact, you can even rejoin your OWN game, then the match will crash 30 seconds later!

    That said, what is the proper way to do it, preferably using HLAPI?

    I'd love a @Unity dev's opinion since there are countless versions of this exact thread -- all unanswered and left lingering what the best way is.

    My current experiment (works for client, not for server):

    Code (CSharp):
    1. // : NetworkLobbyManager
    2. if (this.lobbySlots[mySlot].isServer) // Is slot ALWAYS 0 for localPlayer?
    3.     Network.Disconnect(); // Testing..[/COLOR][B] [COLOR=#ff0000](EDIT: Failed)[/COLOR][/B][/COLOR][/COLOR][/COLOR]
    4. [COLOR=#000000][COLOR=#000000][COLOR=#000000][COLOR=#000000]else
    5.     this.StopHost(); // Works fine for client, cleans up well
    ________________________________________________________

    What does NOT work - usually resulting in the following err (from a NetworkLobbyManager script):
    A connection has already been set as ready. There can only be one.
    ClientScene::AddPlayer: playerControllerId of 0 already in use.

    1. Network.Disconnect()
    2. this.StopHost()
    3. this.StopServer() // Why wouldn't this work..? It only makes sense :p
    ________________________________________________________

    Thoughts:

    • One of the above probably works without HLAPI, but HLAPI does "clean up" for you. It's probably some order-of-operations thing where a HLAPI callback is being triggered and attempting to re-enter the lobby BEFORE things are fully disconnected. Unfortunately, again, this is something that we'd be able to code in 10 seconds if it was actually documented in the API docs (cough, @Unity).
     
    Last edited: Dec 15, 2016
  2. MrLucid72

    MrLucid72

    Joined:
    Jan 12, 2016
    Posts:
    988
    Bump -- this.StopHost() is still the only way I've discovered so far, which works on both server and client, BUT although the client will be kicked out immediately and the server leaves the match immediately, the old match lingers on, shows 1 player left (so others can join the to-be-destroyed match thinking it's an open, new match), and then ~30s later the match awkwardly crashes.

    Bonus question:
    I know host-migration is confirmed broken in-game, but does host migration work on Lobby? There's 15 max players in my match. What if 14 players queue up and the host decides to leave - kicks out 14 players? O__o
     
  3. lejean

    lejean

    Joined:
    Jul 4, 2013
    Posts:
    392
    You need to unlist the match.

    When both lobby players send a ready message the server calls OnLobbyServerPlayersReady.
    This is a good moment to call
    Code (CSharp):
    1. matchMaker.SetMatchAttributes(matchInfo.networkId, false, matchInfo.domain, OnSetMatchAttributes);
    Also host migration only works for lan matches I believe
     
    MrLucid72 likes this.