Skip to content

Commit

Permalink
feat: NetworkClient raises event after authentication (#96)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lymdun committed Mar 22, 2020
1 parent 3b95477 commit c332271
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 41 deletions.
35 changes: 35 additions & 0 deletions Assets/Mirror/Runtime/NetworkClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public class NetworkClient : MonoBehaviour
[Serializable] public class NetworkConnectionEvent : UnityEvent<NetworkConnectionToServer> { }

public NetworkConnectionEvent Connected = new NetworkConnectionEvent();
public NetworkConnectionEvent Authenticated = new NetworkConnectionEvent();

/// <summary>
/// The NetworkConnection object this client is using.
Expand Down Expand Up @@ -124,6 +125,11 @@ public class NetworkClient : MonoBehaviour
/// </summary>
public bool isLocalClient => hostServer != null;

void Start()
{
InitializeAuthEvents();
}

/// <summary>
/// Connect client to a NetworkServer instance.
/// </summary>
Expand Down Expand Up @@ -173,6 +179,7 @@ internal void ConnectHost(NetworkServer server)
if (LogFilter.Debug) Debug.Log("Client Connect Host to Server");

RegisterSystemHandlers(true);
InitializeAuthEvents();

connectState = ConnectState.Connected;

Expand Down Expand Up @@ -205,6 +212,22 @@ void InitializeTransportHandlers()
Transport.activeTransport.OnClientError.AddListener(OnError);
}

void InitializeAuthEvents()
{
if (authenticator != null)
{
authenticator.OnStartClient();
authenticator.OnClientAuthenticated += OnAuthenticated;

Connected.AddListener(authenticator.OnClientAuthenticateInternal);
}
else
{
// if no authenticator, consider connection as authenticated
Connected.AddListener(OnAuthenticated);
}
}

void OnError(Exception exception)
{
Debug.LogException(exception);
Expand Down Expand Up @@ -246,6 +269,14 @@ void OnConnected()
Connected.Invoke((NetworkConnectionToServer)connection);
}

void OnAuthenticated(NetworkConnectionToServer conn)
{
// set connection to authenticated
conn.isAuthenticated = true;

Authenticated?.Invoke(conn);
}

/// <summary>
/// Disconnect from server.
/// <para>The disconnect message will be invoked.</para>
Expand Down Expand Up @@ -412,6 +443,10 @@ public void Shutdown()

connectState = ConnectState.None;
handlers.Clear();

if (authenticator != null)
authenticator.OnClientAuthenticated -= OnAuthenticated;

// disconnect the client connection.
// we do NOT call Transport.Shutdown, because someone only called
// NetworkClient.Shutdown. we can't assume that the server is
Expand Down
42 changes: 1 addition & 41 deletions Assets/Mirror/Runtime/NetworkManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -318,12 +318,6 @@ public void StartClient(string serverIp)

Initialize();

if (client.authenticator != null)
{
client.authenticator.OnStartClient();
client.authenticator.OnClientAuthenticated += OnClientAuthenticated;
}

isNetworkActive = true;

RegisterClientMessages();
Expand Down Expand Up @@ -351,12 +345,6 @@ public void StartClient(Uri uri)

Initialize();

if (client.authenticator != null)
{
client.authenticator.OnStartClient();
client.authenticator.OnClientAuthenticated += OnClientAuthenticated;
}

isNetworkActive = true;

RegisterClientMessages();
Expand Down Expand Up @@ -479,12 +467,6 @@ void StartHostClient()
{
if (LogFilter.Debug) Debug.Log("NetworkManager ConnectLocalClient");

if (client.authenticator != null)
{
client.authenticator.OnStartClient();
client.authenticator.OnClientAuthenticated += OnClientAuthenticated;
}

server.ActivateHostScene();

// ConnectLocalServer needs to be called AFTER RegisterClientMessages
Expand Down Expand Up @@ -536,9 +518,6 @@ public void StopServer()
/// </summary>
public void StopClient()
{
if (client.authenticator != null)
client.authenticator.OnClientAuthenticated -= OnClientAuthenticated;

OnStopClient();

if (LogFilter.Debug) Debug.Log("NetworkManager StopClient");
Expand Down Expand Up @@ -639,7 +618,7 @@ void RegisterServerMessages()

void RegisterClientMessages()
{
client.Connected.AddListener(OnClientConnectInternal);
client.Authenticated.AddListener(OnClientAuthenticated);
client.RegisterHandler<DisconnectMessage>(OnClientDisconnectInternal, false);
client.RegisterHandler<NotReadyMessage>(OnClientNotReadyMessageInternal);
client.RegisterHandler<ErrorMessage>(OnClientErrorInternal, false);
Expand Down Expand Up @@ -1048,30 +1027,11 @@ void OnServerErrorInternal(NetworkConnection conn, ErrorMessage msg)

#region Client Internal Message Handlers

void OnClientConnectInternal(NetworkConnectionToServer conn)
{
if (LogFilter.Debug) Debug.Log("NetworkManager.OnClientConnectInternal");

if (client.authenticator != null)
{
// we have an authenticator - let it handle authentication
client.authenticator.OnClientAuthenticateInternal(conn);
}
else
{
// authenticate immediately
OnClientAuthenticated(conn);
}
}

// called after successful authentication
void OnClientAuthenticated(NetworkConnection conn)
{
if (LogFilter.Debug) Debug.Log("NetworkManager.OnClientAuthenticated");

// set connection to authenticated
conn.isAuthenticated = true;

// proceed with the login handshake by calling OnClientConnect
string loadedSceneName = SceneManager.GetActiveScene().name;
if (string.IsNullOrEmpty(onlineScene) || onlineScene == offlineScene || loadedSceneName == onlineScene)
Expand Down

0 comments on commit c332271

Please sign in to comment.