Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Unity Multiplayer Have a new client disconnect the previous one when connecting

Discussion in 'Multiplayer' started by Onizuka101, Jun 3, 2016.

  1. Onizuka101


    Jul 10, 2015
    Hi everyone,
    I have been using Unity for a while now, but I am pretty new to Unet. I am working on an app that is actually 2 separate projects, where one is the server and one is a client and controls what happens on the server side.
    Everything was going fine and I was able to connect them and control the server using the Unet [command]. The problem I am encountering is the following:
    When a second controller (client) connects to the server it should display a message saying that a client is already connected to it and if it would like to take control. pressing the Yes button should disconnect the first client and connect the second one.
    I have been using different instructions to try to disconnect a client correctly but I keep getting some exception. Even though it actually seem to disconnect the first client and take control, when I try to reconnect the first client to boot the second it doesn't work. those exceptions seem to prevent any other connection from happening.

    here is what I am using to disconnect the first client:

    Code (CSharp):
    1. public override void OnServerConnect(NetworkConnection conn)
    2.     {
    3.         base.OnServerConnect(conn);
    4.         totalConnections++; //count how many clients connect
    6.         if (totalConnections > 1)
    7.         {
    8.             Debug.Log("Too many controllers! disconnecting other controller");
    10.             NetworkServer.connections[NetworkServer.connections.Count-2].Disconnect();
    12.         }
    13.     }
    15. public override void OnServerDisconnect(NetworkConnection conn)
    16.     {
    17.         base.OnServerDisconnect(conn);
    18.         totalConnections--;
    19.         Debug.Log(totalConnections);
    20.     }
    these are the errors I get when the first client disconnects

    Failed to send internal buffer channel:0 bytesToSend:28
    Send Error: 2 channel:0 bytesToSend:28
    Empty player list given to NetworkServer.Destroy(), nothing to do.
    ATMNetworkManager:OnServerDisconnect(NetworkConnection) (at Assets/Scripts/ATMNetworkManager.cs:39)

    I would like to know what the correct way to disconnect or replace a connection is. theoretically I would need to be able to flawlessly switch between clients any amount of times.

    thank you

    it actually seems like as soon as I reconnect the client that was just disconnected it disconnects it again instead of the client that was controlling the server. like if the connections list does not get updated when it goes through disconnecting a client. Im sorry if this whole post sounds confusing, but this is exactly how I am feeling lol
    Last edited: Jun 3, 2016
  2. aabramychev


    Unity Technologies

    Jul 17, 2012
    @Onizuka101 Not sure, but on the first look it is a bug. Please report them, with explanation how we can reproduce this.