Skip to content

Commit

Permalink
Fix SocketConnection.cs
Browse files Browse the repository at this point in the history
  • Loading branch information
lotuuu committed Jun 11, 2024
1 parent 10db0d4 commit 6454cac
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
8 changes: 4 additions & 4 deletions client/Assets/Scripts/BackendConnection/ServerSelect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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"];
Expand All @@ -41,14 +41,14 @@ 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()
{
ServerSelect.Name = "CUSTOM";
ServerSelect.Domain = customServerDomain.text;
await SocketConnection.Instance.CloseConnection();
SocketConnection.Instance.Init();
SocketConnection.Instance.ConnectToSession();
}
}
22 changes: 15 additions & 7 deletions client/Assets/Scripts/BackendConnection/SocketConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand All @@ -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;
Expand All @@ -66,6 +73,7 @@ private void ConnectToSession()
Debug.LogError("Received error: " + e);
};
ws.Connect();
connected = true;
}

private void OnWebsocketClose(WebSocketCloseCode closeCode)
Expand Down

0 comments on commit 6454cac

Please sign in to comment.