I'm trying to create my first multiplayer game using UNET where one of the players is always hosting. I planned to do host migration to keep the game going even if the host leaves. Is this even a good idea in general?
So far I have a function that creates a new room (players starts to host a room), which looks like this:
public void Test_StartHost(){
if (!NetworkClient.active && !NetworkServer.active && manager.matchMaker == null)
{
string ip = Network.player.ipAddress.Replace (".", "|");
string port = Network.player.port.ToString();
db.save(ip + "<<" + port);
NetworkManager.StartHost ();
}
}
This is tested and working (host's game starts and database receives the data). I've tested hosting on both Android and PC. Both of the devices send different IP (xxx.xxx.xx.xxx) but same port 0 to the database. Can the port 0 be correct?
Next thing that happens is that another player tries to join the game as client. For that I have this code:
public void Play()
{
if (!NetworkClient.active && !NetworkServer.active && manager.matchMaker == null)
{
string hostIP = database.get("hostIP"); // xxx.xxx.xx.xxx
int hostPort = int.Parse(database.get("hostPort")); // 0
Debug.Log("Trying to join IP " + hostIP + " as a client. Listening to port " + hostPort + ".");
// prints "Trying to join IP xxx.xxx.xx.xxx as a client. Listening to port 0.
NetworkManager.networkAddress = hostIP;
//NetworkManager.networkPort = hostPort;
NetworkManager.StartClient();
}
}
Joining the game works IF both of the devices are in the same Wi-Fi. If PC (host) uses Wi-Fi and Android tries to join the room (using mobile network) I always get timeout error after ~15 seconds.
Why does this work only on the same Wi-Fi and not over different internet connections?