When i start host it calls OnServerConnect twice. difference is conn.address; localClient and localServer. what is the idea of this? i expect OnServerConnect for each client connect.
Still happening, bumping. I digged around the source files, and found that directly in the
StartHost(ConnectionConfig config, int maxConnections) method, it calls OnServerConnect, and in the LocalClient that the NetworkManager creates, in the
InternalConnectLocalServer(), it also sends the same Connect message, just locally.
A workaround, is to override StartHost() and call it like this:
public override NetworkClient StartHost(ConnectionConfig config, int maxConnections)
{
OnStartHost();
if (StartServer(config, maxConnections))
{
var client = ConnectLocalClient();
OnStartClient(client);
return client;
}
return null;
}
Edit: I should say, that all the StartHost() methods will call the StartHost method I listed first.
Edit 3: Disregard the first edit, the StartHost() methods calls the StartServer method that corresponds to it, which means you need to override the StartHost() method that matches the one you’re calling, and call the StartServer method that matches. More info in the source posted above.
So I was messing around with the NetworkManager again today, and realized that a better alternative might actually be to use the OnServerReady instead, and check if the scene name matches the online scene. OnServerReady is called whenever the scene has been changed, which includes the initial scene change. If you just have a single scene for connecting and playing, it should also call OnServerReady. The good thing is that OnServerReady is still called with a NetworkConnection.
Still though, it would be nice to have this bug fixed!
I think there is another bug with Host. when i call client.disconnect for local player it will not call public override void OnServerDisconnect(NetworkConnection conn). is it normal?
p.s. it calls public override void OnClientDisconnect(NetworkConnection conn) for this client on the Host.
I’ve submitted this OP issue on the Issue Tracker as a bug, not sure when it will be picked up and seen but at least it has been logged. I was having the same problem and thought it was my doing at first, though it seems to be StartHost() makes that extra call on its own. For now an easy way to ensure your OnServerConnect() code isn’t performed twice when the local client is connected, try something like:
private bool localClientFirstConnect = true;
public override void OnServerConnect (NetworkConnection conn)
{
if(conn.connectionId != 0 && numPlayers != 0)
{
Debug.Log("A remote client has connected!");
Debug.Log(conn.connectionId.ToString());
}else if(conn.connectionId == 0 && numPlayers == 0 && localClientFirstConnect)
{
Debug.Log("A local client has connected!");
Debug.Log(conn.connectionId.ToString());
localClientFirstConnect = false;
}else
{
Debug.Log("Caught the local client's second OnServerConnect call.");
}
}
It worked for me, as the first time the OnServerConnect() is called it will allow the code to be run properly for the local client’s connection hook, but when it tries to make a second pass ( which is the bug ) it will catch the second pass happening and will know to not do anything.