51-Mirror__Network Authenticator-NewNetworkAuthenticator.cs.txt 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using Mirror;
  5. using UnityEngine;
  6. /*
  7. Documentation: https://mirror-networking.gitbook.io/docs/components/network-authenticators
  8. API Reference: https://mirror-networking.com/docs/api/Mirror.NetworkAuthenticator.html
  9. */
  10. public class #SCRIPTNAME# : NetworkAuthenticator
  11. {
  12. #region Messages
  13. public struct AuthRequestMessage : NetworkMessage { }
  14. public struct AuthResponseMessage : NetworkMessage { }
  15. #endregion
  16. #region Server
  17. /// <summary>
  18. /// Called on server from StartServer to initialize the Authenticator
  19. /// <para>Server message handlers should be registered in this method.</para>
  20. /// </summary>
  21. public override void OnStartServer()
  22. {
  23. // register a handler for the authentication request we expect from client
  24. NetworkServer.RegisterHandler<AuthRequestMessage>(OnAuthRequestMessage, false);
  25. }
  26. /// <summary>
  27. /// Called on server from OnServerConnectInternal when a client needs to authenticate
  28. /// </summary>
  29. /// <param name="conn">Connection to client.</param>
  30. public override void OnServerAuthenticate(NetworkConnectionToClient conn) { }
  31. /// <summary>
  32. /// Called on server when the client's AuthRequestMessage arrives
  33. /// </summary>
  34. /// <param name="conn">Connection to client.</param>
  35. /// <param name="msg">The message payload</param>
  36. public void OnAuthRequestMessage(NetworkConnectionToClient conn, AuthRequestMessage msg)
  37. {
  38. AuthResponseMessage authResponseMessage = new AuthResponseMessage();
  39. conn.Send(authResponseMessage);
  40. // Accept the successful authentication
  41. ServerAccept(conn);
  42. }
  43. #endregion
  44. #region Client
  45. /// <summary>
  46. /// Called on client from StartClient to initialize the Authenticator
  47. /// <para>Client message handlers should be registered in this method.</para>
  48. /// </summary>
  49. public override void OnStartClient()
  50. {
  51. // register a handler for the authentication response we expect from server
  52. NetworkClient.RegisterHandler<AuthResponseMessage>(OnAuthResponseMessage, false);
  53. }
  54. /// <summary>
  55. /// Called on client from OnClientConnectInternal when a client needs to authenticate
  56. /// </summary>
  57. public override void OnClientAuthenticate()
  58. {
  59. AuthRequestMessage authRequestMessage = new AuthRequestMessage();
  60. NetworkClient.Send(authRequestMessage);
  61. }
  62. /// <summary>
  63. /// Called on client when the server's AuthResponseMessage arrives
  64. /// </summary>
  65. /// <param name="msg">The message payload</param>
  66. public void OnAuthResponseMessage(AuthResponseMessage msg)
  67. {
  68. // Authentication has been accepted
  69. ClientAccept();
  70. }
  71. #endregion
  72. }