Search Unity

MasterServer PollHostList not working

Discussion in 'Multiplayer' started by demonpants, Dec 1, 2008.

  1. demonpants

    demonpants

    Joined:
    Oct 3, 2008
    Posts:
    82
    I tried implementing the MasterServer into our game, but it didn't seem to be working. Thinking maybe it got bogged down or messed up by some of our existing networking code, I basically copied the Unity examples verbatim into a separate script. Still no luck.

    The code is below. I've tested this directly on my machine (two copies running separately), as well as with someone several hundred miles away. No luck. The list always seems to have 0 results, and because MasterServer is such a blackbox I have very little idea what's going on. I have tried hosting from different machines.

    Below is the code:

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class MasterServerTest : MonoBehaviour
    6. {
    7.     void Awake()
    8.     {
    9.         // Make sure list is empty and request a new list
    10.         MasterServer.ClearHostList();
    11.         MasterServer.RequestHostList("ABCTest");
    12.     }
    13.    
    14.     void OnGUI()
    15.     {
    16.         if (GUILayout.Button ("Start Server"))
    17.         {
    18.             // Use NAT punchthrough if no public IP present
    19.             Network.useNat = !Network.HavePublicAddress();
    20.             Network.InitializeServer(32, 25002);
    21.             MasterServer.RegisterHost("ABCTest", "Test game");
    22.         }
    23.     }
    24.    
    25.     void Update()
    26.     {
    27.         // If any hosts were received, display game name, the clear host list again
    28.         if (MasterServer.PollHostList().Length != 0)
    29.         {
    30.             HostData[] hostData = MasterServer.PollHostList();
    31.             for (int i = 0; i<hostData.Length; i++)
    32.             {
    33.                 Debug.Log("Game name: " + hostData[i].gameName);
    34.             }
    35.             MasterServer.ClearHostList();
    36.         }
    37.     }
    38. }
    39.  
    Any ideas what I'm doing wrong? Also if there's some way to get some decent status reporting from the master server, that would be nice as well. The only feedback I ever get is:

    Also if someone could just paste in some source code / a project file for something that successfully connects to the master server, that might help as well.

    Thanks.

    Also I should note that it might appear as if only requesting the server list on Awake() would be the problem, but it's not. I moved that line of code around, and also would host on a separate computer before starting another copy of the program (at which point Awake would occur after the host was created).
     
  2. demonpants

    demonpants

    Joined:
    Oct 3, 2008
    Posts:
    82
    Well I tinkered with it more and simply adding a refresh button seemed to make it work somehow. Apparently I was misunderstanding how the PollHostList function works... I assumed it to always return all connected servers, but I guess clearing the list causes problems, which is logical I suppose. It's just slightly misleading that it references a list you already found with a different function, rather than actually doing any work of its own.

    I'm guessing the issue had something to do with the amount of time it actually takes for the server to be registered. Doing the following:
    Code (csharp):
    1.  
    2. MasterServer.RegisterHost(gameName, "Test game", "l33t game for all");
    3. MasterServer.RequestHostList(gameName);
    4.  
    Won't actually work, because the change obviously isn't instantaneous. Something like this, however, will:
    Code (csharp):
    1.  
    2. if (GUILayout.Button("Host"))
    3. {
    4. MasterServer.RegisterHost(gameName, "Test game", "l33t game for all");
    5. timeHostWasRegistered = Time.time;
    6. }
    7. if (Time.time - timeHostWasRegistered >= 5.0f)
    8. {
    9. MasterServer.RequestHostList(gameName);
    10. }
    11.  
    So anyway maybe if someone stumbles on this that will help them in the future.
     
  3. avatar

    avatar

    Joined:
    May 14, 2009
    Posts:
    3
    I have the same problem, and you are right,
    I also added delay and it help.
    Interesting if it's only because the table not created yet, and will be no problem on second server connection.

    Anyway: you can see exactly what Master server or ConnTester is doing, every his transaction, at least on Linux, just run it on terminal (SSH or TTY)
    not as a background process, nor as service,
    then it will display all his transactions, including database queries, on the screen.
     
  4. Hardcore Games

    Hardcore Games

    Joined:
    Jan 29, 2013
    Posts:
    8
    Remove the MasterServer.ClearHostList();

    Remember that Update is always running. The second time it ran, it found no games registered.