Created
September 17, 2018 04:13
-
-
Save SoftwareGuy/1ff0468330a8ad5a32f142340221a689 to your computer and use it in GitHub Desktop.
Example Auth System for Mirror/Classic UNET
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This is an example way of making an auth system for Mirror/Classic UNET. | |
// This is a "kickstart" script that may or may not work for you. NO WARRANTY. | |
// Code written by SoftwareGuy/Coburn64 (@coburn64 on Twitter). Please don't claim as your own. | |
// START SNIPPET // | |
using UnityEngine; | |
using Mirror; // Change this to "using UnityEngine.Networking" if using classic UNET | |
public class CustomNetworkManager : NetworkManager { | |
private int EXAMPLE_AUTH_PACKET_ID = 1000; | |
public override void OnStartServer() { | |
base.OnStartServer(); | |
// Register a handler for the auth packet ID. | |
// The client will send this to the server. | |
NetworkServer.RegisterHandler(EXAMPLE_AUTH_PACKET_ID, HandleAuthPacket); | |
} | |
public override void OnStartClient(NetworkClient client){ | |
base.OnStartClient(client); | |
// Send an auth connection packet | |
client.Send(EXAMPLE_AUTH_PACKET_ID, new AuthPacket(){ authToken = "Your_Clients_Auth_Token" }); | |
} | |
public override void HandleAuthPacket(NetworkMessage netMsg) { | |
AuthPacket ap = netMsg.ReadMessage<AuthPacket>(); | |
if(ap != null) { | |
// You can now access the auth packet's contents. | |
// An example is below: | |
Debug.LogWarning("Auth Packet get from " + netMsg.connection.address); | |
Debug.LogWarning("Auth Packet Token: " + ap.authToken); | |
// You would then do something with that. Send it up stream to accounts server, send a message back to the client saying they're nongenuine, etc. | |
// For example, to cut the connection: | |
// netMsg.connection.Close(); | |
} | |
} | |
} | |
public class AuthPacket : MessageBase { | |
public string authToken = string.Empty; | |
} | |
// END SNIPPET // | |
If you have any issues you can either contact me on Discord or Twitter. No, I'm not going to write your netcode for you. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment