Search Unity

replicating networkmanager from scratch

Discussion in 'Multiplayer' started by Noktai, Jun 23, 2015.

  1. Noktai

    Noktai

    Joined:
    Feb 11, 2013
    Posts:
    40
    I'm trying to replicate the functionality of the networkmanager. Partially so I have a better understanding what's going on behind the screen, and also because I want a bit more freedom.

    The functionality I try to a chieve is this;
    • host server
    • connect client
    • load server state (previously spawned gameobjects and stuff)
    • clean up on a disconnect, and allow for re-connect
    Sounds pretty simple, but the documentation is pretty lacking. All the documentation assumes you're using the networkmanager.

    So far I have this
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.Networking;
    4.  
    5. public class CustomNetworkManager : MonoBehaviour {
    6.  
    7.     public GameObject playerPrefab;
    8.     NetworkClient myClient;
    9.  
    10.     bool isHost = false;
    11.  
    12.     // Use this for initialization
    13.     void Start () {
    14.    
    15.     }
    16.    
    17.     // Update is called once per frame
    18.     void Update () {
    19.    
    20.     }
    21.  
    22.     // Create a server and listen on a port
    23.     public void SetupServer()
    24.     {
    25.         NetworkServer.Listen(4444);
    26.         //isAtStartup = false;
    27.     }
    28.  
    29.     public void SetupHost()
    30.     {
    31.         SetupServer();
    32.         SetupClient();
    33.  
    34.         isHost = true;
    35.     }
    36.    
    37.     // Create a client and connect to the server port
    38.     public void SetupClient()
    39.     {
    40.         myClient = new NetworkClient();
    41.         myClient.RegisterHandler(MsgType.Connect, OnConnected);
    42.         myClient.RegisterHandler( MsgType.Disconnect, OnDisconnect );
    43.        
    44.         myClient.Connect("127.0.0.1", 4444);
    45.     }
    46.  
    47.     // client function
    48.     public void OnConnected(NetworkMessage netMsg)
    49.     {
    50.         Debug.Log("Connected to server");
    51.        
    52.         //ClientScene.Ready( myClient.connection ); //no idea what this does or when to call it
    53.  
    54.         ClientScene.RegisterPrefab( playerPrefab );
    55.  
    56.         GameObject player = (GameObject)GameObject.Instantiate( playerPrefab );
    57.         NetworkServer.Spawn( player );
    58.  
    59.         //CameraManager.instance.AddFocusObj( player );
    60.     }
    61.  
    62.     public void OnDisconnect(NetworkMessage netMsg)
    63.     {
    64.         Debug.Log("discconected");
    65.     }
    66.  
    67.     void OnGUI()
    68.     {
    69.         if( NetworkClient.active == false && NetworkServer.active == false)
    70.         {
    71.             if( GUILayout.Button("start host" ) )
    72.             {
    73.                 SetupHost();
    74.             }
    75.             if( GUILayout.Button("start server")  )
    76.             {
    77.                 SetupServer();
    78.             }
    79.             if( GUILayout.Button("start client" ) )
    80.             {
    81.                 SetupClient();
    82.             }
    83.         }
    84.         else
    85.         {
    86.             if( GUILayout.Button("disconnect"))
    87.             {
    88.                 Debug.Log("attempt to disconnect");
    89.                 //none of these actually trigger OnDiscconect
    90.                 //myClient.Shutdown();
    91.                 //Network.Disconnect();
    92.                 myClient.Disconnect();
    93.  
    94.                 if( isHost ) NetworkServer.Shutdown();
    95.             }
    96.         }
    97.     }
    98.  
    99.     void OnDisconnectedFromServer(NetworkDisconnection info) {
    100.         if (Network.isServer)
    101.             Debug.Log("Local server connection disconnected");
    102.         else
    103.             if (info == NetworkDisconnection.LostConnection)
    104.                 Debug.Log("Lost connection to the server");
    105.         else
    106.             Debug.Log("Successfully diconnected from the server");
    107.     }
    108. }
    109.  
    There's a few things going wrong
    • Disconnection callback is not called ( myClient.RegisterHandler( MsgType.Disconnect, OnDisconnect ); )
    • State is not synchronized upon connection, I know it has something to do with ClientScene.Ready. But the documentation is pretty vague about this. I end up with 2 local players when I enable it.
    Any help?
     
  2. seanr

    seanr

    Unity Technologies

    Joined:
    Sep 22, 2014
    Posts:
    669
    you will need separate event handlers for the server and client events - such as for connect and disconnect. Use NetworkServer.RegisterHandler to register for server events.