fix: finally fixed the websocket problem, apparently there's no multi-threaded support

This commit is contained in:
2025-12-12 11:36:06 -05:00
Unverified
parent 6567f79a32
commit b373822d0e
3 changed files with 20 additions and 52 deletions

View File

@@ -11,7 +11,7 @@ config_version=5
[application] [application]
config/name="Connect4 Observer" config/name="Connect4 Observer"
config/version="0.2.0" config/version="1.0.0"
run/main_scene="uid://dcx5nvs0pa7me" run/main_scene="uid://dcx5nvs0pa7me"
config/features=PackedStringArray("4.5", "C#", "Forward Plus") config/features=PackedStringArray("4.5", "C#", "Forward Plus")
boot_splash/image="uid://dd7lvnidxr5ss" boot_splash/image="uid://dd7lvnidxr5ss"

View File

@@ -20,10 +20,10 @@ metadata/_snap_enabled = true
anchors_preset = 15 anchors_preset = 15
anchor_right = 1.0 anchor_right = 1.0
anchor_bottom = 1.0 anchor_bottom = 1.0
offset_left = -659.0 offset_left = -658.0
offset_top = -558.0 offset_top = -547.0
offset_right = 619.0 offset_right = 668.0
offset_bottom = 466.0 offset_bottom = 657.0
grow_horizontal = 2 grow_horizontal = 2
grow_vertical = 2 grow_vertical = 2
texture = SubResource("GradientTexture2D_yqjtg") texture = SubResource("GradientTexture2D_yqjtg")

View File

@@ -58,6 +58,7 @@ public partial class Connection : Node
private bool _connected = false; private bool _connected = false;
private List<(string, int)> _lastScoreboard = []; private List<(string, int)> _lastScoreboard = [];
private bool _shouldShowTournamentResults = false; private bool _shouldShowTournamentResults = false;
private float _refreshGamePlayerListTimer = 5.0f;
public override void _Ready() public override void _Ready()
{ {
@@ -83,11 +84,6 @@ public partial class Connection : Node
} }
} }
public override void _ExitTree()
{
StopGameListRefreshLoop();
}
public override void _Process(double delta) public override void _Process(double delta)
{ {
_webSocket.Poll(); _webSocket.Poll();
@@ -100,7 +96,18 @@ public partial class Connection : Node
_connected = true; _connected = true;
LastError = ""; LastError = "";
OnWsConnectionSuccess?.Invoke(); OnWsConnectionSuccess?.Invoke();
StartGameListRefreshLoop(); UpdateGameList();
UpdatePlayerList();
_refreshGamePlayerListTimer = 5.0f;
} else if (_refreshGamePlayerListTimer <= 0.0f)
{
UpdateGameList();
UpdatePlayerList();
_refreshGamePlayerListTimer = 5.0f;
}
else
{
_refreshGamePlayerListTimer -= (float) delta;
} }
while (_webSocket.GetAvailablePacketCount() > 0) while (_webSocket.GetAvailablePacketCount() > 0)
@@ -144,7 +151,7 @@ public partial class Connection : Node
CurrentWaitTimeout = 5.0; CurrentWaitTimeout = 5.0;
ActiveTournament = false; ActiveTournament = false;
DemoMode = false; DemoMode = false;
StopGameListRefreshLoop(); _refreshGamePlayerListTimer = 5.0f;
var code = _webSocket.GetCloseCode(); var code = _webSocket.GetCloseCode();
var reason = _webSocket.GetCloseReason(); var reason = _webSocket.GetCloseReason();
LastError = "Unexpected Disconnect. Reason: " + reason + ", Code: " + code; LastError = "Unexpected Disconnect. Reason: " + reason + ", Code: " + code;
@@ -190,45 +197,6 @@ public partial class Connection : Node
SendCommand("GAME", "LIST"); SendCommand("GAME", "LIST");
} }
private void StartGameListRefreshLoop()
{
if (_gameListThreadRunning)
{
return;
}
_gameListThreadRunning = true;
_gameListThread = new Thread(() =>
{
while (_gameListThreadRunning)
{
if (IsSocketOpen)
{
UpdateGameList();
UpdatePlayerList();
}
Thread.Sleep(TimeSpan.FromSeconds(5));
}
})
{
IsBackground = true
};
_gameListThread.Start();
}
private void StopGameListRefreshLoop()
{
if (!_gameListThreadRunning)
{
return;
}
_gameListThreadRunning = false;
_gameListThread?.Join();
_gameListThread = null;
}
public void UpdatePlayerList() public void UpdatePlayerList()
{ {
SendCommand("PLAYER", "LIST"); SendCommand("PLAYER", "LIST");
@@ -433,8 +401,8 @@ public partial class Connection : Node
} }
case "START": case "START":
{ {
OnStartTournamentAck?.Invoke();
ActiveTournament = true; ActiveTournament = true;
OnStartTournamentAck?.Invoke();
break; break;
} }
case "CANCEL": case "CANCEL":