diff --git a/client/Assets/Scripts/BackendConnection/ServerSelect.cs b/client/Assets/Scripts/BackendConnection/ServerSelect.cs index 2a6c26ab..bcfb9c78 100644 --- a/client/Assets/Scripts/BackendConnection/ServerSelect.cs +++ b/client/Assets/Scripts/BackendConnection/ServerSelect.cs @@ -25,8 +25,8 @@ void Awake() if (string.IsNullOrEmpty(ServerSelect.Name) || string.IsNullOrEmpty(ServerSelect.Domain)) { #if UNITY_EDITOR - ServerSelect.Name = "LOCALHOST"; - ServerSelect.Domain = servers["LOCALHOST"]; + ServerSelect.Name = "LOCALHOST"; + ServerSelect.Domain = servers["LOCALHOST"]; #else ServerSelect.Name = "EUROPE"; ServerSelect.Domain = servers["EUROPE"]; @@ -41,7 +41,7 @@ public async void SelectServer(string domainName) ServerSelect.Domain = servers[domainName]; serverButtonText.text = ServerSelect.Name; await SocketConnection.Instance.CloseConnection(); - SocketConnection.Instance.Init(); + SocketConnection.Instance.ConnectToSession(); } public async void SelectCustomServer() @@ -49,6 +49,6 @@ public async void SelectCustomServer() ServerSelect.Name = "CUSTOM"; ServerSelect.Domain = customServerDomain.text; await SocketConnection.Instance.CloseConnection(); - SocketConnection.Instance.Init(); + SocketConnection.Instance.ConnectToSession(); } } diff --git a/client/Assets/Scripts/BackendConnection/SocketConnection.cs b/client/Assets/Scripts/BackendConnection/SocketConnection.cs index bf10f3f3..79414e9c 100644 --- a/client/Assets/Scripts/BackendConnection/SocketConnection.cs +++ b/client/Assets/Scripts/BackendConnection/SocketConnection.cs @@ -26,13 +26,19 @@ async void Start() public void Init() { - Instance = this; - DontDestroyOnLoad(gameObject); - - if (!connected) + if (Instance == null) + { + Instance = this; + DontDestroyOnLoad(gameObject); + if (!connected) + { + ConnectToSession(); + } + } + else { - connected = true; - ConnectToSession(); + // Destroy this instance if another one already exists + Destroy(gameObject); } } @@ -51,8 +57,9 @@ private async void OnApplicationQuit() await this.CloseConnection(); } - private void ConnectToSession() + public void ConnectToSession() { + Debug.Log("SocketConnection ConnectToSession"); string url = $"{ServerSelect.Domain}/2"; ws = new WebSocket(url); ws.OnMessage += OnWebSocketMessage; @@ -66,6 +73,7 @@ private void ConnectToSession() Debug.LogError("Received error: " + e); }; ws.Connect(); + connected = true; } private void OnWebsocketClose(WebSocketCloseCode closeCode)