Search Unity

How to disconnect a client

Discussion in 'Multiplayer' started by Xuzon, Mar 27, 2017.

  1. Xuzon

    Xuzon

    Joined:
    Mar 21, 2014
    Posts:
    83
    Hello, I've found a little weird behaviour.

    How do you disconnect a client from a server properly? I call NetworkManager.singleton.StopClient() but it throws some errors after disconnection

    host id out of bound id {0} max id should be greater 0 and less than {0}

    And after that, the client cannot connect again to a server until I quit the client app or stop playing and play again in the editor.

    I connect to a Unet dedicated server with a public IP over the internet.
     
  2. aabramychev

    aabramychev

    Unity Technologies

    Joined:
    Jul 17, 2012
    Posts:
    574
    >host id out of bound id {0} max id should be greater 0 and less than {0}
    means that you trying to send something after host was removed...

    for disconnect you need to call disconnect function first, wait couple of frames and then shutdown. If you feel that it is a bug, generate bug report, and attach your project to them.
     
  3. Xuzon

    Xuzon

    Joined:
    Mar 21, 2014
    Posts:
    83
    @aabramychev hey! I've just tried a lot of stuff but I'm still getting the same

    I've tried calling Network.Disconnect() after that start a coroutine that yield for 5 frames and then call StopClient, and nothing, the same, and that output is stacking, in a few minutes I had over 10K messages

    Calling only StopClient() as I'm using the HLAPI only but the same.

    Callin StopClient and then Destroying the network manager just in case but nothing, the same but now it throws only 1 message.

    And there is a strange behaviour also, that when I I call StartClient() or StartHost() always appear this message
    StartClient connects directly directly to a dedicated server

    "A connection has already been set as ready. There can only be one"
     
  4. aabramychev

    aabramychev

    Unity Technologies

    Joined:
    Jul 17, 2012
    Posts:
    574
    @8NeonBitGuy Sorry, I do not understand your explanation.

    You call Disconnect on client and then you received 10k messages for this client? Or you cannot send using this client?
    Anyway the question looks like, I cannot answer without code access. Could you generate simple project and open bug for this? Sorry, for inconvenience :(
     
  5. Xuzon

    Xuzon

    Joined:
    Mar 21, 2014
    Posts:
    83
    @aabramychev With the 10k messages I mean 10K in the console this
    "host id out of bound id {0} max id should be greater 0 and less than {0}"

    I've found a weird solution but it works
    Code (CSharp):
    1. public void Disconnect() {
    2.         MyNetManager netManager = NetworkManager.singleton as MyNetManager;
    3.         if (isServer) {
    4.             netManager.StopHost();
    5.             Destroy(NetworkManager.singleton.gameObject);
    6.         } else {
    7.             NetworkClient.ShutdownAll();
    8.             netManager.StopClient();
    9.             //Destroy all the other clients
    10.             foreach(Player p in Player.playersList) {
    11.                 if(p != null && p != this) {
    12.                     Destroy(p.gameObject);
    13.                 }
    14.             }
    15.             //then destroy me
    16.             Destroy(gameObject);
    17.         }
    18.     }
    If I do it in this way I can connect to another server perfectly
    If you are wondering why I destroy them manually it is because they remain there inactive, even if I'm disconnected, I don't know why :/
     
    wlwl2 and Kurius like this.
  6. aabramychev

    aabramychev

    Unity Technologies

    Joined:
    Jul 17, 2012
    Posts:
    574
    @8NeonBitGuy Hmm, actually looks like a bug for me, could you report them, please? (I understood it will have time and appreciate this)