I've been searching through the forum and have found a few threads talking about Pinging game servers from a Lobby to get their latency but I'm stuck and am hoping someone can help me see what mistake I've made.
- Here is what my code looks like
public void SetupServer(){
udpPort = UnityEngine.Random.Range(10000, 40000);
if (TNServerInstance.Start(5127, udpPort, null, TNServerInstance.Type.Tcp, Tools.ResolveEndPoint("xx.xxx.xx.xx:5129")))
{
TNManager.Connect();
}
}
void OnConnect(bool result, string message)
{
if (result)
{
TNManager.JoinChannel(1, null, false, 5128, null);
}
else
{
LoadingScreen.Instance.HideLoadingScreen();
MessageWindow.DisplayMessageWindow(message);
}
}
private void OnJoinChannel(int channelID, bool success, string message)
{
if (success)
{
if (TNManager.isHosting)
{
TNManager.Instantiate(1, "NetworkControllerCreate", "NetworkController", false);
TNManager.SetPlayerLimit(4); // Only allow 4 players in an online match
// Make it possible to use UDP using a random port
if (!TNServerInstance.isLocal && TNManager.isHosting)
TNManager.StartUDP(udpPort);
}
}
else
{
LoadingScreen.Instance.HideLoadingScreen();
MessageWindow.DisplayMessageWindow(message);
}
}
I then attach the udpPort to my server name and parse it out when accessing in my serverBrowser script:
public void UpdateServerList()
{
Debug.Log("Updating server list");
// Clean out previous items
foreach (Transform child in Content)
{
GameObject.Destroy(child.gameObject);
}
pingDictionary.Clear();
// List of discovered servers
TNet.List<ServerList.Entry> list = TNLobbyClient.knownServers.list;
// Server list example script automatically collects servers that have recently announced themselves
for (int i = 0; i < list.size; ++i)
{
//Debug.Log("Server " + i);
TNet.ServerList.Entry ent = list[i];
var go = Instantiate(ServerListOption, Vector3.zero, Quaternion.identity, Content);
var listOption = go.GetComponent<ServerListOption>();
//var serverDetails = JsonUtility.FromJson<ServerDetails>(ent.name);
var serverDetails = ent.name.Split('|');
listOption.Ip = ent.externalAddress.ToString();
listOption.Name.text = serverDetails[0];
listOption.GameType.text = serverDetails[1];
listOption.Map.text = serverDetails[2];
int udpPort = int.Parse(serverDetails[3]);
listOption.PlayerCount.text = ent.playerCount.ToString() + "/4";
pingDictionary.Add(ent.externalAddress, listOption.Ping);
TNManager.Ping(Tools.ResolveEndPoint(ent.externalAddress.Address.ToString(), udpPort), PingCallback);
}
}
However I always get the error:"The socket is null. Did you forget to call UdpProtocol.Start()?"
I'm calling TNManager.StartUDP with the same port number that my ServerBrowser script then tries to use when pinging the game server....
I'm using TNTcpLobbyClient, do I need to be using TNUdpLobbyClient instead?
I have my Lobby server hosted on Amazon Web Services
Any other ideas what I might be doing wrong?