Search Unity

Client connecting twice

Discussion in 'Multiplayer' started by vandi13, Mar 9, 2017.

  1. vandi13

    vandi13

    Joined:
    Mar 6, 2017
    Posts:
    1
    Hello and thanks for your time in advance,

    I have a simple client/server architecture which just doesn't work.

    The problem: If a client connects (testing from localhost) the server creates 2 connections for the client and the NetworkServer.connections.count raises by 2, which makes it impossible to work correctly.

    What happens: everything works fine on the clientside (I think), it's only sending the "connecting" and "connected" messages once, but the server opens two connections and I don't get why...

    I left some unimportant code out because it would be too much to read I guess.

    I would be so thankful if you guys could help me out.

    My script so far:

    Server

    Code (CSharp):
    1.  
    2. public class ServerBehaviour : MonoBehaviour {
    3.     NetworkClient myClient;
    4.     bool running=false;
    5.     int clientCount=0;
    6.     // Message IDs
    7.     public static short spawnMobMsgID = 10000;
    8.     public static short pingID = 10001;
    9.  
    10. public void startServer(){
    11.         if (!running) {
    12.             NetworkServer.Listen (1337);
    13.             NetworkServer.RegisterHandler (spawnMobMsgID, onSpawnMobMessage);
    14.             NetworkServer.RegisterHandler (pingID, onPing);
    15.             running = true;
    16.             Debug.Log ("server running");
    17.         }
    18.     }
    19.  
    20. public void stopServer(){
    21.         if (running) {
    22.             NetworkServer.Shutdown ();
    23.             running = false;
    24.             Debug.Log ("server stopped");
    25.         }
    26.  
    27.     }
    28.  
    29. void Update () {
    30.  
    31.         int connectedClients = NetworkServer.connections.Count;
    32.         if (connectedClients > clientCount) {
    33.             Debug.Log ("connection id " + NetworkServer.connections [connectedClients - 1].connectionId + " connected");
    34.             clientCount++;
    35.              }
    36. }
    37. }
    38.  

    and the client...

    Code (CSharp):
    1.  
    2. public class Networking : MonoBehaviour {
    3.     NetworkClient myClient;
    4.     bool connected=false;
    5.  
    6.     //messageIDs
    7.     public static short spawnMobMsgId = 10000;
    8.     public static short pingID = 10001;
    9.     // Use this for initialization
    10.     void Start () {
    11.        
    12.     }
    13.  
    14. public void connect(){
    15.         if (!connected) {
    16.             connected = true;
    17.             myClient = new NetworkClient ();
    18.             myClient.RegisterHandler (MsgType.Connect, OnConnected);  
    19.             myClient.RegisterHandler (pingID, onPing);
    20.             myClient.Connect ("127.0.0.1", 1337);
    21.             Debug.Log ("connecting");
    22.         }
    23.     }
    24.  
    25. void OnConnected(NetworkMessage netMsg)
    26.     {
    27.         Debug.Log("Connected to server");
    28.         Debug.Log ("connection id" + myClient.connection.connectionId);
    29.     }
    30.     // Update is called once per frame
    31.     void Update () {
    32.        
    33.     }
    34. }
    35.  
    36.  
     
  2. musashi255

    musashi255

    Joined:
    May 4, 2017
    Posts:
    2
    having same issue and i'm using ServerSimple. do i really need to * all my indexes by 2? or is there just one dummy connection in the 0 index and i need to +1? Ah the things we do to make unity function normally.