mirror of
https://github.com/DerTyp7/defrain-shooter-unity.git
synced 2025-10-29 04:42:13 +01:00
89 lines
2.7 KiB
Plaintext
89 lines
2.7 KiB
Plaintext
using UnityEngine;
|
|
using UnityEngine.SceneManagement;
|
|
using Mirror;
|
|
|
|
/*
|
|
Documentation: https://mirror-networking.gitbook.io/docs/components/network-authenticators
|
|
API Reference: https://mirror-networking.com/docs/api/Mirror.NetworkAuthenticator.html
|
|
*/
|
|
|
|
public class #SCRIPTNAME# : NetworkAuthenticator
|
|
{
|
|
#region Messages
|
|
|
|
public struct AuthRequestMessage : NetworkMessage { }
|
|
|
|
public struct AuthResponseMessage : NetworkMessage { }
|
|
|
|
#endregion
|
|
|
|
#region Server
|
|
|
|
/// <summary>
|
|
/// Called on server from StartServer to initialize the Authenticator
|
|
/// <para>Server message handlers should be registered in this method.</para>
|
|
/// </summary>
|
|
public override void OnStartServer()
|
|
{
|
|
// register a handler for the authentication request we expect from client
|
|
NetworkServer.RegisterHandler<AuthRequestMessage>(OnAuthRequestMessage, false);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Called on server from OnServerAuthenticateInternal when a client needs to authenticate
|
|
/// </summary>
|
|
/// <param name="conn">Connection to client.</param>
|
|
public override void OnServerAuthenticate(NetworkConnection conn) { }
|
|
|
|
/// <summary>
|
|
/// Called on server when the client's AuthRequestMessage arrives
|
|
/// </summary>
|
|
/// <param name="conn">Connection to client.</param>
|
|
/// <param name="msg">The message payload</param>
|
|
public void OnAuthRequestMessage(NetworkConnection conn, AuthRequestMessage msg)
|
|
{
|
|
AuthResponseMessage authResponseMessage = new AuthResponseMessage();
|
|
|
|
conn.Send(authResponseMessage);
|
|
|
|
// Accept the successful authentication
|
|
ServerAccept(conn);
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Client
|
|
|
|
/// <summary>
|
|
/// Called on client from StartClient to initialize the Authenticator
|
|
/// <para>Client message handlers should be registered in this method.</para>
|
|
/// </summary>
|
|
public override void OnStartClient()
|
|
{
|
|
// register a handler for the authentication response we expect from server
|
|
NetworkClient.RegisterHandler<AuthResponseMessage>(OnAuthResponseMessage, false);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Called on client from OnClientAuthenticateInternal when a client needs to authenticate
|
|
/// </summary>
|
|
public override void OnClientAuthenticate()
|
|
{
|
|
AuthRequestMessage authRequestMessage = new AuthRequestMessage();
|
|
|
|
NetworkClient.Send(authRequestMessage);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Called on client when the server's AuthResponseMessage arrives
|
|
/// </summary>
|
|
/// <param name="msg">The message payload</param>
|
|
public void OnAuthResponseMessage(AuthResponseMessage msg)
|
|
{
|
|
// Authentication has been accepted
|
|
ClientAccept();
|
|
}
|
|
|
|
#endregion
|
|
}
|