client: now handles disconnects properly and notifies the user of server connect and disconnect

This commit is contained in:
2025-07-04 12:22:48 +02:00
parent 2aa6d710cf
commit b468a391f5
2 changed files with 29 additions and 12 deletions
+6 -2
View File
@@ -56,6 +56,8 @@ func _on_web_client_tcp_connected_to_server() -> void:
name_input.editable = true
lobby_code_input.editable = true
_write_line("You are connected to the server")
func _on_web_client_tcp_connection_error_occurred() -> void:
connect_button.disabled = false
@@ -68,13 +70,15 @@ func _on_web_client_tcp_disconnected_from_server() -> void:
name_input.editable = true
lobby_code_input.editable = false
lobby_code_input.text = ""
_write_line("You were disconnected from the server")
func _on_web_client_tcp_connected_to_lobby(lobby_id: String) -> void:
lobby_code_input.text = lobby_id
_write_line("you connected to lobby {lobby_id}".format({"lobby_id": lobby_id}))
_write_line("You connected to lobby {lobby_id}".format({"lobby_id": lobby_id}))
func _on_web_client_tcp_disconnected_from_lobby() -> void:
_write_line("you disconnected from the lobby")
_write_line("You disconnected from the lobby")
func _on_web_client_tcp_client_joined_lobby(client_name: String) -> void:
_write_line("{client_name} joined the lobby{you}".format({
+23 -10
View File
@@ -10,7 +10,8 @@ var _lobby_id: String
var _client_id: String
var _client_name: String
var received_data = ""
var received_data: Array
var cached_response: String
var _thread: Thread
var _stop_thread: bool = false
@@ -44,10 +45,12 @@ func _process(_delta: float) -> void:
if not _processed_status == _new_status:
match _new_status:
StreamPeerTCP.Status.STATUS_NONE:
_clean_up_thread()
connection_status_changed.emit(ConnectionStatus.DISCONNECTED)
StreamPeerTCP.Status.STATUS_CONNECTING:
connection_status_changed.emit(ConnectionStatus.ATTEMPTING_CONNECTION)
StreamPeerTCP.Status.STATUS_CONNECTED:
_stop_thread = false
_thread = Thread.new()
_thread.start(_listen_for_data)
connected_to_server.emit()
@@ -60,12 +63,19 @@ func _process(_delta: float) -> void:
func _listen_for_data() -> void:
while not _stop_thread and not _tcp.get_status() == StreamPeerTCP.Status.STATUS_ERROR:
received_data += _tcp.get_utf8_string(1)
print(received_data)
if received_data.ends_with("}"):
_handle_response(received_data)
received_data = ""
while not _stop_thread:
received_data = _tcp.get_data(1)
# Error (e.g. abrupt disconnect), stop listening
if not received_data[0] == OK:
disconnected_from_server.emit.call_deferred()
return
cached_response += (received_data[1] as PackedByteArray).get_string_from_utf8()
if cached_response.ends_with("}"):
print(cached_response)
_handle_response(cached_response)
cached_response = ""
func _handle_response(response: String) -> void:
var data = JSON.parse_string(response)
@@ -99,15 +109,18 @@ func _exit_tree() -> void:
func connect_to_server() -> void:
_tcp.connect_to_host("127.0.0.1", 9974)
_tcp.set_no_delay(true)
connected_to_server.emit()
func disconnect_from_server() -> void:
_tcp.disconnect_from_host()
_clean_up_thread()
disconnected_from_server.emit()
connection_status_changed.emit(ConnectionStatus.DISCONNECTED)
func _clean_up_thread() -> void:
_stop_thread = true
if _thread:
_thread.wait_to_finish()
disconnected_from_server.emit()
connection_status_changed.emit(ConnectionStatus.DISCONNECTED)
_thread = null
func create_lobby(client_name: String) -> void:
_client_name = client_name