- Home /
Unexpected `MasterServerEvent.RegistrationSucceeded` on player connect/disconnect
Every time a player connects and disconnects from the server: On the server `OnMasterServerEvent(MasterServerEvent mse)` is called. mse = `MasterServerEvent.RegistrationSucceeded`.
Is this expected behavior?
I am testing with a "Build and Run" instance and the Unity editor play instance.
This is highly problematic as I use it tell whether the server registered without error.
`Network.OnServerInitialized()` only tells me the server set up locally. But I also need to know if registered successfully.
Why am I getting a lot of Registration Succeeded messages?
How do I get around this?
Answer by MLM · Mar 13, 2014 at 05:54 PM
This is a easy fix but I still think this may be a bug. All you have to do is make your own bool to check if you already registered. Set it true when you register and set it false when you unregister. Only fire the event if the variable is false, then set it to true.
Here is some code that applies to solving the issue:
private bool serverRegistered;
public bool ServerRegistered
{
get {
return serverRegistered;
}
private set {
this.serverRegistered = value;
}
}
public void Disconnect()
{
// Manual function to disconnect
Debug.Log("Disconnected Server: " + Network.isServer);
//this.HideServer();
Network.Disconnect();
MasterServer.UnregisterHost();
this.ServerRegistered = false;
}
void OnMasterServerEvent(MasterServerEvent mse)
{
// Only send the event if it pertains to registering a server
if(mse != MasterServerEvent.HostListReceived)
{
Debug.Log("Registered Server: " + mse);
// Only fire the event once
if(!this.ServerRegistered)
this.ThisRegisterServerAttempt(mse); // Fire Event
this.ServerRegistered = true;
}
}