From b377d765ec36c8352ec24c5ed2f20fd92dbbf64e Mon Sep 17 00:00:00 2001 From: MrGadget1024 <9826063+MrGadget1024@users.noreply.github.com> Date: Tue, 28 Mar 2023 13:13:17 -0400 Subject: [PATCH] fix(Chat Example): moved playerNames HashSet to ChatAuthenticator --- .../Chat/Scripts/ChatAuthenticator.cs | 19 +++++++++++-------- .../Chat/Scripts/ChatNetworkManager.cs | 2 +- Assets/Mirror/Examples/Chat/Scripts/Player.cs | 13 ++----------- 3 files changed, 14 insertions(+), 20 deletions(-) diff --git a/Assets/Mirror/Examples/Chat/Scripts/ChatAuthenticator.cs b/Assets/Mirror/Examples/Chat/Scripts/ChatAuthenticator.cs index c1265dd88..f678b3c61 100644 --- a/Assets/Mirror/Examples/Chat/Scripts/ChatAuthenticator.cs +++ b/Assets/Mirror/Examples/Chat/Scripts/ChatAuthenticator.cs @@ -13,6 +13,7 @@ namespace Mirror.Examples.Chat public class ChatAuthenticator : NetworkAuthenticator { readonly HashSet connectionsPendingDisconnect = new HashSet(); + internal static readonly HashSet playerNames = new HashSet(); [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(); + } + /// /// Called on server from StartServer to initialize the Authenticator /// Server message handlers should be registered in this method. @@ -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() /// public override void OnClientAuthenticate() { - AuthRequestMessage authRequestMessage = new AuthRequestMessage - { - authUsername = playerName, - }; - - NetworkClient.Send(authRequestMessage); + NetworkClient.Send(new AuthRequestMessage { authUsername = playerName }); } /// diff --git a/Assets/Mirror/Examples/Chat/Scripts/ChatNetworkManager.cs b/Assets/Mirror/Examples/Chat/Scripts/ChatNetworkManager.cs index 947a5f113..a575eafed 100644 --- a/Assets/Mirror/Examples/Chat/Scripts/ChatNetworkManager.cs +++ b/Assets/Mirror/Examples/Chat/Scripts/ChatNetworkManager.cs @@ -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); diff --git a/Assets/Mirror/Examples/Chat/Scripts/Player.cs b/Assets/Mirror/Examples/Chat/Scripts/Player.cs index 253c004a0..2ee8c8d79 100644 --- a/Assets/Mirror/Examples/Chat/Scripts/Player.cs +++ b/Assets/Mirror/Examples/Chat/Scripts/Player.cs @@ -5,17 +5,8 @@ namespace Mirror.Examples.Chat { public class Player : NetworkBehaviour { - internal static readonly HashSet playerNames = new HashSet(); - - [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() {