Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[GH-448] Fix multiple SocketConnections in Overworld #489

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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
Loading