fix(Chat Example): moved playerNames HashSet to ChatAuthenticator

This commit is contained in:
MrGadget1024 2023-03-28 13:13:17 -04:00
parent 48b2ee638f
commit b377d765ec
3 changed files with 14 additions and 20 deletions

View File

@ -13,6 +13,7 @@ namespace Mirror.Examples.Chat
public class ChatAuthenticator : NetworkAuthenticator
{
readonly HashSet<NetworkConnection> connectionsPendingDisconnect = new HashSet<NetworkConnection>();
internal static readonly HashSet<string> playerNames = new HashSet<string>();
[Header("Client Username")]
public string playerName;
@ -36,6 +37,13 @@ public struct AuthResponseMessage : NetworkMessage
#region Server
// RuntimeInitializeOnLoadMethod -> fast playmode without domain reload
[UnityEngine.RuntimeInitializeOnLoadMethod]
static void ResetStatics()
{
playerNames.Clear();
}
/// <summary>
/// Called on server from StartServer to initialize the Authenticator
/// <para>Server message handlers should be registered in this method.</para>
@ -77,10 +85,10 @@ public void OnAuthRequestMessage(NetworkConnectionToClient conn, AuthRequestMess
if (connectionsPendingDisconnect.Contains(conn)) return;
// check the credentials by calling your web server, database table, playfab api, or any method appropriate.
if (!Player.playerNames.Contains(msg.authUsername))
if (!playerNames.Contains(msg.authUsername))
{
// Add the name to the HashSet
Player.playerNames.Add(msg.authUsername);
playerNames.Add(msg.authUsername);
// Store username in authenticationData
// This will be read in Player.OnStartServer
@ -170,12 +178,7 @@ public override void OnStopClient()
/// </summary>
public override void OnClientAuthenticate()
{
AuthRequestMessage authRequestMessage = new AuthRequestMessage
{
authUsername = playerName,
};
NetworkClient.Send(authRequestMessage);
NetworkClient.Send(new AuthRequestMessage { authUsername = playerName });
}
/// <summary>

View File

@ -27,7 +27,7 @@ public override void OnServerDisconnect(NetworkConnectionToClient conn)
{
// remove player name from the HashSet
if (conn.authenticationData != null)
Player.playerNames.Remove((string)conn.authenticationData);
ChatAuthenticator.playerNames.Remove((string)conn.authenticationData);
// remove connection from Dictionary of conn > names
ChatUI.connNames.Remove(conn);

View File

@ -5,17 +5,8 @@ namespace Mirror.Examples.Chat
{
public class Player : NetworkBehaviour
{
internal static readonly HashSet<string> playerNames = new HashSet<string>();
[SerializeField, SyncVar]
internal string playerName;
// RuntimeInitializeOnLoadMethod -> fast playmode without domain reload
[UnityEngine.RuntimeInitializeOnLoadMethod]
static void ResetStatics()
{
playerNames.Clear();
}
[SyncVar]
public string playerName;
public override void OnStartServer()
{