- Home /
Question by
Zahiro · Nov 02, 2016 at 06:31 PM ·
networkingclientmasterserverconnectip
How do i connect to a host that is registered on the MasterServer?
I'm making a matchmaking system for my game but i realized that i don't know how to retrieve connection info from a host within the MasterServer so i'm stuck .
private string charactersToRandom = "0123456789qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM";
private string generatedServerName = "";
private int randomInteger;
void Start () {
MasterServer.RequestHostList ("ThisIsMyGame");
Invoke("Connector", 3);
}
void Connector () {
if(MasterServer.PollHostList ().Length == 0){
StartupHost ();
Debug.Log ("Sessions available : " + MasterServer.PollHostList ().Length);
}
else {
Debug.Log ("Sessions available : " + MasterServer.PollHostList ().Length);
HostData[] hostData = MasterServer.PollHostList ();
JoinGame (hostData [0].gameName);
int i = 0;
while (i < hostData.Length) {
Debug.Log ("Game name: " + hostData [i].gameName);
i++;
}
}
}
public void StartupHost () {
GenerateServerName ();
bool useNat = !Network.HavePublicAddress ();
Network.InitializeServer (4, 3781, useNat);
MasterServer.RegisterHost ("ThisIsMyGame", generatedServerName);
NetworkManager.singleton.StartHost ();
Debug.Log ("Host");
}
public void JoinGame (string ipToConnect) {
Network.Connect (ipToConnect, 3781);
NetworkManager.singleton.StartClient ();
Debug.Log ("Client");
}
//SERVER NAME GENERATOR
void GenerateServerName () {
for (int i = 0; i < 25; i++) {
randomInteger = Random.Range (0, charactersToRandom.Length);
generatedServerName = generatedServerName + charactersToRandom [randomInteger];
}
}
}
Comment