Files
defrain-shooter-unity/Assets/ScriptTemplates/51-Mirror__Network Authenticator-NewNetworkAuthenticator.cs.txt
2021-10-25 09:20:01 +02:00

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
}