Hello, I browsed all relevant network issue related posts, but I could not find an answer.
I build a custom master server, build with vs2008, without touching the code. I run it on my pc.
I have a unity exe that starts as a server with following c# code:
public class ServerController : MonoBehaviour
{
void Start()
{
DebugConsole.IsOpen = true;
DebugConsole.Log("Server started");
}
void OnGUI()
{
if (GUILayout.Button("Start Server"))
{
Network.InitializeServer(32, 23467, false);
MasterServer.ipAddress = "127.0.0.1";
MasterServer.RegisterHost("GameName", "ZeGame", "l33t game for all");
}
}
void OnPlayerConnected(NetworkPlayer player)
{
DebugConsole.Log("Player " + player + " connected from " + player.ipAddress + ":" + player.port);
}
void OnServerInitialized()
{
DebugConsole.Log("Server is initialized");
}
void OnPlayerDisconnected(NetworkPlayer player)
{
Debug.Log("Clean up after player " + player);
Network.RemoveRPCs(player);
Network.DestroyPlayerObjects(player);
}
}
And a client that runs following script:
public class ClientController : MonoBehaviour
{
void Start()
{
MasterServer.ipAddress = "127.0.0.1";
MasterServer.RequestHostList("WimiAmericanRoulette");
DebugConsole.IsOpen = true;
DebugConsole.Log("Client started");
}
void OnConnectedToServer()
{
DebugConsole.Log("Connected to server");
DebugConsole.Log("network: " + Network.peerType);
}
void OnDisconnectedFromServer(NetworkDisconnection info)
{
if (info == NetworkDisconnection.LostConnection)
DebugConsole.Log("Lost connection to the server");
else
DebugConsole.Log("Successfully disconnected from the server");
}
void OnFailedToConnect(NetworkConnectionError error)
{
DebugConsole.Log("Could not connect to server: " + error);
}
void OnFailedToConnectToMasterServer(NetworkConnectionError info)
{
DebugConsole.Log("Could not connect to master server: " + info);
}
void OnGUI()
{
if (GUILayout.Button("Refresh"))
{
MasterServer.ClearHostList();
MasterServer.RequestHostList("WimiAmericanRoulette");
}
HostData[] data = MasterServer.PollHostList();
// Go through all the hosts in the host list
foreach (HostData element in data)
{
GUILayout.BeginHorizontal();
string name = element.gameName + " " + element.connectedPlayers + " / " + element.playerLimit;
GUILayout.Label(name);
GUILayout.Space(5);
string hostInfo;
hostInfo = "[";
foreach (string host in element.ip)
hostInfo = hostInfo + host + ":" + element.port + " ";
hostInfo = hostInfo + "]";
GUILayout.Label(hostInfo);
GUILayout.Space(5);
GUILayout.Label(element.comment);
GUILayout.Space(5);
GUILayout.FlexibleSpace();
if (GUILayout.Button("Connect"))
{
// Connect to HostData struct, internally the correct method is used (GUID when using NAT).
DebugConsole.Log("Connecting..." + hostInfo);
NetworkConnectionError error = Network.Connect(element);
if (error != NetworkConnectionError.NoError)
{
DebugConsole.Log("Failed to connect " + error);
}
else
{
DebugConsole.Log("network: " + Network.peerType);
}
}
if (GUILayout.Button("Disconnect"))
{
// Connect to HostData struct, internally the correct method is used (GUID when using NAT).
Network.Disconnect();
DebugConsole.Log("network: " + Network.peerType);
}
GUILayout.EndHorizontal();
}
if (GUILayout.Button("Send \"Hello World\""))
{
networkView.RPC("RPCTest", RPCMode.Others, "Hello world");
}
}
}
I can start the server, OnInitialized gets called.
Then I start the client, I receive the game from the masterserver that is running on the server. I do the Network.Connect call: it immediatly returns, in state ‘disconnected’ and then it takes an arbitrary time to connect. Sometimes it’s round 20 seconds, other times it takes minutes. I have the impression that it helps to switch focus between server and client.
I would expect that having focus helps, but in an earlier version I could connect in a split second without having to switch focus, so that makes me think I’m doing something wrong.
And them when I disconnect, the OnPlayerDisconnected call is naver made on the server. Again, in an earlier version this was triggered in a split second.
I’m unable to find what I broke, so I hope that somebody can tell me what is wrong with the code. It is all taken from the samples.
I probably must mention that these scripts are attached to the maincamera of an empty scene (each a different scene) with a networkview also attached to the maincamera. (when connected, the position of the camera gets synced, as expected).
Many hanks for any help i can get,
Alex