client: now handles disconnects properly and notifies the user of server connect and disconnect
This commit is contained in:
@@ -56,6 +56,8 @@ func _on_web_client_tcp_connected_to_server() -> void:
|
|||||||
|
|
||||||
name_input.editable = true
|
name_input.editable = true
|
||||||
lobby_code_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:
|
func _on_web_client_tcp_connection_error_occurred() -> void:
|
||||||
connect_button.disabled = false
|
connect_button.disabled = false
|
||||||
@@ -68,13 +70,15 @@ func _on_web_client_tcp_disconnected_from_server() -> void:
|
|||||||
name_input.editable = true
|
name_input.editable = true
|
||||||
lobby_code_input.editable = false
|
lobby_code_input.editable = false
|
||||||
lobby_code_input.text = ""
|
lobby_code_input.text = ""
|
||||||
|
|
||||||
|
_write_line("You were disconnected from the server")
|
||||||
|
|
||||||
func _on_web_client_tcp_connected_to_lobby(lobby_id: String) -> void:
|
func _on_web_client_tcp_connected_to_lobby(lobby_id: String) -> void:
|
||||||
lobby_code_input.text = lobby_id
|
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:
|
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:
|
func _on_web_client_tcp_client_joined_lobby(client_name: String) -> void:
|
||||||
_write_line("{client_name} joined the lobby{you}".format({
|
_write_line("{client_name} joined the lobby{you}".format({
|
||||||
|
|||||||
@@ -10,7 +10,8 @@ var _lobby_id: String
|
|||||||
var _client_id: String
|
var _client_id: String
|
||||||
var _client_name: String
|
var _client_name: String
|
||||||
|
|
||||||
var received_data = ""
|
var received_data: Array
|
||||||
|
var cached_response: String
|
||||||
|
|
||||||
var _thread: Thread
|
var _thread: Thread
|
||||||
var _stop_thread: bool = false
|
var _stop_thread: bool = false
|
||||||
@@ -44,10 +45,12 @@ func _process(_delta: float) -> void:
|
|||||||
if not _processed_status == _new_status:
|
if not _processed_status == _new_status:
|
||||||
match _new_status:
|
match _new_status:
|
||||||
StreamPeerTCP.Status.STATUS_NONE:
|
StreamPeerTCP.Status.STATUS_NONE:
|
||||||
|
_clean_up_thread()
|
||||||
connection_status_changed.emit(ConnectionStatus.DISCONNECTED)
|
connection_status_changed.emit(ConnectionStatus.DISCONNECTED)
|
||||||
StreamPeerTCP.Status.STATUS_CONNECTING:
|
StreamPeerTCP.Status.STATUS_CONNECTING:
|
||||||
connection_status_changed.emit(ConnectionStatus.ATTEMPTING_CONNECTION)
|
connection_status_changed.emit(ConnectionStatus.ATTEMPTING_CONNECTION)
|
||||||
StreamPeerTCP.Status.STATUS_CONNECTED:
|
StreamPeerTCP.Status.STATUS_CONNECTED:
|
||||||
|
_stop_thread = false
|
||||||
_thread = Thread.new()
|
_thread = Thread.new()
|
||||||
_thread.start(_listen_for_data)
|
_thread.start(_listen_for_data)
|
||||||
connected_to_server.emit()
|
connected_to_server.emit()
|
||||||
@@ -60,12 +63,19 @@ func _process(_delta: float) -> void:
|
|||||||
|
|
||||||
|
|
||||||
func _listen_for_data() -> void:
|
func _listen_for_data() -> void:
|
||||||
while not _stop_thread and not _tcp.get_status() == StreamPeerTCP.Status.STATUS_ERROR:
|
while not _stop_thread:
|
||||||
received_data += _tcp.get_utf8_string(1)
|
received_data = _tcp.get_data(1)
|
||||||
print(received_data)
|
|
||||||
if received_data.ends_with("}"):
|
# Error (e.g. abrupt disconnect), stop listening
|
||||||
_handle_response(received_data)
|
if not received_data[0] == OK:
|
||||||
received_data = ""
|
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:
|
func _handle_response(response: String) -> void:
|
||||||
var data = JSON.parse_string(response)
|
var data = JSON.parse_string(response)
|
||||||
@@ -99,15 +109,18 @@ func _exit_tree() -> void:
|
|||||||
func connect_to_server() -> void:
|
func connect_to_server() -> void:
|
||||||
_tcp.connect_to_host("127.0.0.1", 9974)
|
_tcp.connect_to_host("127.0.0.1", 9974)
|
||||||
_tcp.set_no_delay(true)
|
_tcp.set_no_delay(true)
|
||||||
connected_to_server.emit()
|
|
||||||
|
|
||||||
func disconnect_from_server() -> void:
|
func disconnect_from_server() -> void:
|
||||||
_tcp.disconnect_from_host()
|
_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
|
_stop_thread = true
|
||||||
if _thread:
|
if _thread:
|
||||||
_thread.wait_to_finish()
|
_thread.wait_to_finish()
|
||||||
disconnected_from_server.emit()
|
_thread = null
|
||||||
connection_status_changed.emit(ConnectionStatus.DISCONNECTED)
|
|
||||||
|
|
||||||
func create_lobby(client_name: String) -> void:
|
func create_lobby(client_name: String) -> void:
|
||||||
_client_name = client_name
|
_client_name = client_name
|
||||||
|
|||||||
Reference in New Issue
Block a user