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:
public override void OnServerConnect(NetworkConnection conn)
{
base.OnServerConnect(conn);
totalConnections++; //count how many clients connect
if (totalConnections > 1)
{
Debug.Log("Too many controllers! disconnecting other controller");
NetworkServer.connections[NetworkServer.connections.Count-2].Disconnect();
}
}
public override void OnServerDisconnect(NetworkConnection conn)
{
base.OnServerDisconnect(conn);
totalConnections--;
Debug.Log(totalConnections);
}
these are the errors I get when the first client disconnects
Failed to send internal buffer channel:0 bytesToSend:28
UnityEngine.Networking.NetworkIdentity:UNetStaticUpdate()
Send Error: 2 channel:0 bytesToSend:28
UnityEngine.Networking.NetworkIdentity:UNetStaticUpdate()
Empty player list given to NetworkServer.Destroy(), nothing to do.
UnityEngine.Networking.NetworkManager:OnServerDisconnect(NetworkConnection)
ATMNetworkManager:OnServerDisconnect(NetworkConnection) (at Assets/Scripts/ATMNetworkManager.cs:39)
UnityEngine.Networking.NetworkIdentity:UNetStaticUpdate()
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
EDIT:
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