client: added ConnectionStatus to invoke UI changes when connection status changes

This commit is contained in:
2025-07-02 21:58:19 +02:00
parent d128f39e6a
commit f084a599b7
3 changed files with 65 additions and 2 deletions
+18 -2
View File
@@ -24,6 +24,16 @@ signal disconnected_from_lobby()
signal client_joined_lobby(client_name: String)
signal connection_status_changed(new_status: ConnectionStatus)
enum ConnectionStatus {
DISCONNECTED,
ATTEMPTING_CONNECTION,
LOBBYLESS, # == connected
ATTEMPTING_JOIN,
JOINED,
}
func _process(delta: float) -> void:
_tcp.poll()
_new_status = _tcp.get_status()
@@ -31,15 +41,17 @@ func _process(delta: float) -> void:
if not _processed_status == _new_status:
match _new_status:
StreamPeerTCP.Status.STATUS_NONE:
pass
connection_status_changed.emit(ConnectionStatus.DISCONNECTED)
StreamPeerTCP.Status.STATUS_CONNECTING:
pass
connection_status_changed.emit(ConnectionStatus.ATTEMPTING_CONNECTION)
StreamPeerTCP.Status.STATUS_CONNECTED:
_thread = Thread.new()
_thread.start(_listen_for_data)
connected_to_server.emit()
connection_status_changed.emit(ConnectionStatus.LOBBYLESS)
StreamPeerTCP.Status.STATUS_ERROR:
connection_error_occurred.emit()
connection_status_changed.emit(ConnectionStatus.DISCONNECTED)
_processed_status = _new_status
@@ -59,9 +71,11 @@ func _handle_response(response: String) -> void:
"create_lobby":
if data["status"] == "ok":
connected_to_lobby.emit.call_deferred(_lobby_id)
connection_status_changed.emit.call_deferred(ConnectionStatus.JOINED)
"join_lobby":
if data["status"] == "ok":
connected_to_lobby.emit.call_deferred(_lobby_id)
connection_status_changed.emit.call_deferred(ConnectionStatus.JOINED)
"client_joined_lobby":
client_joined_lobby.emit.call_deferred(data["new_client_name"])
@@ -79,6 +93,7 @@ func disconnect_from_server() -> void:
if _thread:
_thread.wait_to_finish()
disconnected_from_server.emit()
connection_status_changed.emit(ConnectionStatus.DISCONNECTED)
func create_lobby(client_name: String) -> void:
_lobby_id = _generate_code()
@@ -102,6 +117,7 @@ func leave_lobby() -> void:
_client_name = ""
disconnected_from_lobby.emit()
connection_status_changed.emit(ConnectionStatus.LOBBYLESS)
func _send_message(message: String) -> void:
_tcp.put_data(message.to_utf8_buffer())