2025-06-29 20:34:46 +02:00
|
|
|
class_name WebClientTCP
|
|
|
|
|
extends Node
|
|
|
|
|
|
2025-07-04 19:59:46 +02:00
|
|
|
#var _tcp := StreamPeerTCP.new()
|
|
|
|
|
var _socket := WebSocketPeer.new()
|
2025-06-29 20:34:46 +02:00
|
|
|
|
2025-07-04 19:59:46 +02:00
|
|
|
var _new_status: int = WebSocketPeer.STATE_CLOSED
|
2025-06-29 20:34:46 +02:00
|
|
|
var _processed_status: int = -1
|
|
|
|
|
|
|
|
|
|
var _lobby_id: String
|
|
|
|
|
var _client_id: String
|
|
|
|
|
var _client_name: String
|
|
|
|
|
|
2025-07-04 12:22:48 +02:00
|
|
|
var received_data: Array
|
|
|
|
|
var cached_response: String
|
2025-06-29 20:34:46 +02:00
|
|
|
|
|
|
|
|
var _thread: Thread
|
|
|
|
|
var _stop_thread: bool = false
|
|
|
|
|
|
2025-07-07 09:58:34 +02:00
|
|
|
var _currently_connected_client_names: Array[String] = []
|
2025-07-06 19:49:08 +02:00
|
|
|
|
2025-06-29 20:34:46 +02:00
|
|
|
signal connected_to_server()
|
|
|
|
|
signal connection_error_occurred()
|
|
|
|
|
signal disconnected_from_server()
|
|
|
|
|
|
|
|
|
|
signal connected_to_lobby(lobby_id: String)
|
|
|
|
|
signal disconnected_from_lobby()
|
|
|
|
|
|
2025-07-07 09:58:34 +02:00
|
|
|
signal client_joined_lobby(client_name: String, currently_connected_client_names: Array[String])
|
|
|
|
|
signal client_left_lobby(client_name: String, currently_connected_client_names: Array[String])
|
2025-06-30 22:07:43 +02:00
|
|
|
|
2025-07-07 10:57:39 +02:00
|
|
|
signal received_connection_message(message: String)
|
2025-07-04 11:29:25 +02:00
|
|
|
signal received_chat_message(client_name: String, message: String)
|
|
|
|
|
|
2025-07-02 21:58:19 +02:00
|
|
|
signal connection_status_changed(new_status: ConnectionStatus)
|
|
|
|
|
|
|
|
|
|
enum ConnectionStatus {
|
|
|
|
|
DISCONNECTED,
|
|
|
|
|
ATTEMPTING_CONNECTION,
|
|
|
|
|
LOBBYLESS, # == connected
|
|
|
|
|
ATTEMPTING_JOIN,
|
|
|
|
|
JOINED,
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-04 11:41:57 +02:00
|
|
|
func _process(_delta: float) -> void:
|
2025-07-04 19:59:46 +02:00
|
|
|
_socket.poll()
|
|
|
|
|
_new_status = _socket.get_ready_state()
|
2025-06-29 20:34:46 +02:00
|
|
|
if not _processed_status == _new_status:
|
|
|
|
|
match _new_status:
|
2025-07-04 19:59:46 +02:00
|
|
|
WebSocketPeer.STATE_OPEN:
|
|
|
|
|
print("open")
|
2025-07-04 12:22:48 +02:00
|
|
|
_stop_thread = false
|
2025-07-06 19:49:08 +02:00
|
|
|
#_thread = Thread.new()
|
|
|
|
|
#_thread.start(_listen_for_data)
|
2025-06-29 20:34:46 +02:00
|
|
|
connected_to_server.emit()
|
2025-07-02 21:58:19 +02:00
|
|
|
connection_status_changed.emit(ConnectionStatus.LOBBYLESS)
|
2025-07-04 19:59:46 +02:00
|
|
|
WebSocketPeer.STATE_CONNECTING:
|
|
|
|
|
print("connecting")
|
|
|
|
|
connection_status_changed.emit(ConnectionStatus.ATTEMPTING_CONNECTION)
|
|
|
|
|
WebSocketPeer.STATE_CLOSING:
|
|
|
|
|
print("closing")
|
|
|
|
|
pass
|
|
|
|
|
WebSocketPeer.STATE_CLOSED:
|
|
|
|
|
print("closed")
|
2025-07-06 19:49:08 +02:00
|
|
|
#_clean_up_thread()
|
2025-07-02 21:58:19 +02:00
|
|
|
connection_status_changed.emit(ConnectionStatus.DISCONNECTED)
|
2025-07-04 19:59:46 +02:00
|
|
|
|
2025-06-29 20:34:46 +02:00
|
|
|
_processed_status = _new_status
|
2025-07-06 19:49:08 +02:00
|
|
|
|
|
|
|
|
if _processed_status == WebSocketPeer.STATE_OPEN:
|
|
|
|
|
while _socket.get_available_packet_count():
|
2025-07-04 19:59:46 +02:00
|
|
|
cached_response = _socket.get_packet().get_string_from_utf8()
|
2025-07-04 12:22:48 +02:00
|
|
|
print(cached_response)
|
2025-07-04 19:59:46 +02:00
|
|
|
#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 = ""
|
2025-06-29 20:34:46 +02:00
|
|
|
|
2025-07-06 19:49:08 +02:00
|
|
|
func _listen_for_data() -> void:
|
|
|
|
|
while true:
|
|
|
|
|
if _socket.get_available_packet_count():
|
|
|
|
|
cached_response = _socket.get_packet().get_string_from_utf8()
|
|
|
|
|
print(cached_response)
|
|
|
|
|
if cached_response.ends_with("}"):
|
|
|
|
|
_handle_response(cached_response)
|
|
|
|
|
cached_response = ""
|
|
|
|
|
|
2025-06-29 20:34:46 +02:00
|
|
|
func _handle_response(response: String) -> void:
|
|
|
|
|
var data = JSON.parse_string(response)
|
|
|
|
|
match data["response"]:
|
|
|
|
|
"create_lobby":
|
|
|
|
|
if data["status"] == "ok":
|
2025-07-04 10:57:24 +02:00
|
|
|
_lobby_id = data["lobby_id"]
|
|
|
|
|
_client_id = data["client_id"]
|
2025-06-29 20:34:46 +02:00
|
|
|
connected_to_lobby.emit.call_deferred(_lobby_id)
|
2025-07-02 21:58:19 +02:00
|
|
|
connection_status_changed.emit.call_deferred(ConnectionStatus.JOINED)
|
2025-07-07 10:57:39 +02:00
|
|
|
"join_lobby_response":
|
|
|
|
|
_handle_join(data)
|
2025-06-30 22:07:43 +02:00
|
|
|
"client_joined_lobby":
|
2025-07-07 09:58:34 +02:00
|
|
|
_currently_connected_client_names.assign(data["connected_client_names"])
|
|
|
|
|
client_joined_lobby.emit.call_deferred(data["new_client_name"], _currently_connected_client_names)
|
|
|
|
|
print(_currently_connected_client_names)
|
2025-07-02 22:16:52 +02:00
|
|
|
"leave_lobby_request":
|
|
|
|
|
leave_lobby()
|
|
|
|
|
"leave_lobby":
|
|
|
|
|
if data["status"] == "ok":
|
2025-07-04 11:41:57 +02:00
|
|
|
client_left_lobby.emit.call_deferred(data["client_name"])
|
|
|
|
|
"client_left_lobby":
|
2025-07-07 09:58:34 +02:00
|
|
|
_currently_connected_client_names.assign(data["connected_client_names"])
|
|
|
|
|
print(_currently_connected_client_names)
|
|
|
|
|
client_left_lobby.emit.call_deferred(data["client_name"], _currently_connected_client_names)
|
2025-07-04 11:29:25 +02:00
|
|
|
"receive_chat_message":
|
|
|
|
|
received_chat_message.emit.call_deferred(data["client_name"], data["message"])
|
2025-06-29 20:34:46 +02:00
|
|
|
|
2025-07-07 10:57:39 +02:00
|
|
|
func _handle_join(data) -> void:
|
|
|
|
|
match data["status"]:
|
|
|
|
|
"lobby_not_found":
|
|
|
|
|
received_connection_message.emit.call_deferred("Could not join; lobby does not exist")
|
|
|
|
|
connection_status_changed.emit.call_deferred(ConnectionStatus.LOBBYLESS)
|
|
|
|
|
"lobby_full":
|
|
|
|
|
received_connection_message.emit.call_deferred("Could not join; lobby is full")
|
|
|
|
|
connection_status_changed.emit.call_deferred(ConnectionStatus.LOBBYLESS)
|
|
|
|
|
"ok":
|
|
|
|
|
_client_id = data["client_id"]
|
|
|
|
|
connected_to_lobby.emit.call_deferred(_lobby_id)
|
|
|
|
|
connection_status_changed.emit.call_deferred(ConnectionStatus.JOINED)
|
|
|
|
|
|
2025-06-29 20:34:46 +02:00
|
|
|
func _exit_tree() -> void:
|
|
|
|
|
disconnect_from_server()
|
|
|
|
|
|
2025-07-06 19:49:08 +02:00
|
|
|
func connect_to_server(text: String) -> void:
|
|
|
|
|
var err = _socket.connect_to_url(text)
|
2025-07-04 19:59:46 +02:00
|
|
|
if err != OK:
|
|
|
|
|
print("didn't work!")
|
2025-06-29 20:34:46 +02:00
|
|
|
|
|
|
|
|
func disconnect_from_server() -> void:
|
2025-07-04 19:59:46 +02:00
|
|
|
_socket.close()
|
2025-07-04 12:22:48 +02:00
|
|
|
disconnected_from_server.emit()
|
|
|
|
|
connection_status_changed.emit(ConnectionStatus.DISCONNECTED)
|
|
|
|
|
|
2025-06-29 20:34:46 +02:00
|
|
|
func create_lobby(client_name: String) -> void:
|
|
|
|
|
_client_name = client_name
|
|
|
|
|
|
2025-07-04 10:57:24 +02:00
|
|
|
_send_message(TCPMessages.create_lobby(client_name))
|
2025-06-29 20:34:46 +02:00
|
|
|
|
|
|
|
|
func join_lobby(lobby_id: String, client_name: String) -> void:
|
|
|
|
|
_lobby_id = lobby_id
|
|
|
|
|
_client_name = client_name
|
|
|
|
|
|
2025-07-04 10:57:24 +02:00
|
|
|
_send_message(TCPMessages.join_lobby(_lobby_id, client_name))
|
2025-07-07 10:57:39 +02:00
|
|
|
connection_status_changed.emit.call_deferred(ConnectionStatus.ATTEMPTING_JOIN)
|
2025-06-29 20:34:46 +02:00
|
|
|
|
|
|
|
|
func leave_lobby() -> void:
|
|
|
|
|
_send_message(TCPMessages.leave_lobby(_lobby_id, _client_id))
|
|
|
|
|
|
|
|
|
|
_lobby_id = ""
|
|
|
|
|
_client_id = ""
|
|
|
|
|
_client_name = ""
|
2025-06-30 22:07:43 +02:00
|
|
|
|
2025-07-04 11:29:25 +02:00
|
|
|
disconnected_from_lobby.emit.call_deferred()
|
|
|
|
|
connection_status_changed.emit.call_deferred(ConnectionStatus.LOBBYLESS)
|
|
|
|
|
|
|
|
|
|
func send_chat_message(message: String) -> void:
|
|
|
|
|
_send_message(TCPMessages.send_chat_message(_lobby_id, _client_id, message))
|
2025-06-29 20:34:46 +02:00
|
|
|
|
|
|
|
|
func _send_message(message: String) -> void:
|
2025-07-04 19:59:46 +02:00
|
|
|
_socket.send_text(message)
|