Search Unity

Server and clients keep reconnecting on disconnect

Discussion in 'Connected Games' started by amherst, Aug 25, 2009.

  1. amherst

    amherst

    Joined:
    Dec 9, 2008
    Posts:
    30
    Whenever I hit "disconnect" on either the server or my clients the server or client reconnects as if it is connecting for the first time. I have the code running in an Awake() function, which I thought only runs once when the script instance is loaded.
    In my code the script gets a value for username from another game object.

    Anybody have any ideas?

    Thanks!
    Code (csharp):
    1.  
    2. var remoteIP = "127.0.0.1";
    3. var remotePort = 25001;
    4. var listenPort = 25000;
    5. var useNAT = false;
    6.  
    7. private var username : String;
    8.  
    9. function Awake()
    10. {
    11.     //if there's a MasterServer running, then we won't run this script
    12.     if (FindObjectOfType(ConnectGuiMasterServer))
    13.         this.enabled = false;
    14.    
    15.     // The username is passed in from another object in the login scene
    16.     usernameObject = GameObject.Find("EmptyUsernameObject");
    17.     username = usernameObject.GetComponent(DontDestroyObject).username;
    18.  
    19.         // if username = "server", start the server
    20.         if (username == "server")
    21.         {
    22.             Debug.Log("running StartServer");
    23.             Network.useNat = useNAT;
    24.             Network.InitializeServer(32, listenPort);   //32 is the max # of connex we'll take
    25.  
    26.             // Notify our spawnpoint that the level and the network is ready
    27.             var sp = GameObject.Find("SpawnPoint");
    28.             sp.SendMessage("OnNetworkLoadedLevel", SendMessageOptions.DontRequireReceiver);
    29.         }
    30.    
    31.         // otherwise connect to the server as a client
    32.         else {
    33.             Network.useNat = useNAT;
    34.             Network.Connect(remoteIP, remotePort);
    35.         }
    36.  
    37. }
    38.  
    39.  
    40. // STILL HAVE A DISCONNECT BUTTON
    41. function OnGUI() {
    42.     GUILayout.Space(10);   
    43.  
    44.     GUILayout.BeginHorizontal(); //everything from here to EndHorizontal will be one after the other witih 10 spaces b/t
    45.     GUILayout.Space(10);
    46.     if (GUILayout.Button("Disconnect"))
    47.     {
    48.         Network.Disconnect(200); //200 is the timeout
    49.     }
    50.     GUILayout.FlexibleSpace();
    51.     GUILayout.EndHorizontal();
    52. }
    53.  
    54.  
    55. // Automatically runs on the client when the client has successfully connected to a server
    56. function OnConnectedToServer() {
    57.     // Notify our objects that the level and the network is ready
    58.     // Specifically, run the function OnNetworkLoadedLevel which is in SpawnPrefab.js
    59.     // This fcn Instantiates a playerPrefab
    60.     for (var go in FindObjectsOfType(GameObject))
    61.         go.SendMessage("OnNetworkLoadedLevel", SendMessageOptions.DontRequireReceiver);    
    62.  
    63. }
    64.  
    65. // Automatically runs on the client when the client has disconnected from server
    66. function OnDisconnectedFromServer () {
    67.     Debug.Log("DISCONNECTING");
    68.     if (this.enabled != false)
    69.         Application.LoadLevel(Application.loadedLevel);
    70.     else
    71.         FindObjectOfType(NetworkLevelLoad).OnDisconnectedFromServer();
    72. }
    73.  
     
  2. LukeB

    LukeB

    Joined:
    Aug 8, 2009
    Posts:
    88
    It looks to me like the OnDisconnectedFromServer() function is the source of your problem. It results in a dodgy loop which would then of course re-run the network.connect :)

    Code (csharp):
    1.  
    2. if (this.enabled != false)
    3. Application.LoadLevel(Application.loadedLevel);
    4.    else
    5.       FindObjectOfType(NetworkLevelLoad).OnDisconnectedFromServer();
    6.  
    7.  
    This basically says 'If this script is running, reload the current level'. You'll probably want to change Application.LoadLevel(Application.loadedLevel); to Application.LoadLevel("Name of login level");