Switched from UDP to TCP-based connection; implemented creating, joining, leaving lobbies
This commit is contained in:
@@ -0,0 +1,112 @@
|
||||
class_name WebClientTCP
|
||||
extends Node
|
||||
|
||||
var _tcp := StreamPeerTCP.new()
|
||||
|
||||
var _new_status: int = -1
|
||||
var _processed_status: int = -1
|
||||
|
||||
var _lobby_id: String
|
||||
var _client_id: String
|
||||
var _client_name: String
|
||||
|
||||
var received_data = ""
|
||||
|
||||
var _thread: Thread
|
||||
var _stop_thread: bool = false
|
||||
|
||||
signal connected_to_server()
|
||||
signal connection_error_occurred()
|
||||
signal disconnected_from_server()
|
||||
|
||||
signal connected_to_lobby(lobby_id: String)
|
||||
signal disconnected_from_lobby()
|
||||
|
||||
func _process(delta: float) -> void:
|
||||
_tcp.poll()
|
||||
_new_status = _tcp.get_status()
|
||||
# Ensure status change is only processed once
|
||||
if not _processed_status == _new_status:
|
||||
match _new_status:
|
||||
StreamPeerTCP.Status.STATUS_NONE:
|
||||
pass
|
||||
StreamPeerTCP.Status.STATUS_CONNECTING:
|
||||
pass
|
||||
StreamPeerTCP.Status.STATUS_CONNECTED:
|
||||
_thread = Thread.new()
|
||||
_thread.start(_listen_for_data)
|
||||
connected_to_server.emit()
|
||||
StreamPeerTCP.Status.STATUS_ERROR:
|
||||
connection_error_occurred.emit()
|
||||
|
||||
_processed_status = _new_status
|
||||
|
||||
|
||||
func _listen_for_data() -> void:
|
||||
while not _stop_thread:
|
||||
#received_data += String.chr(_tcp.get_data(1)[0])
|
||||
received_data += _tcp.get_utf8_string(1)
|
||||
print(received_data)
|
||||
if received_data.ends_with("}"):
|
||||
_handle_response(received_data)
|
||||
received_data = ""
|
||||
|
||||
func _handle_response(response: String) -> void:
|
||||
var data = JSON.parse_string(response)
|
||||
match data["response"]:
|
||||
"create_lobby":
|
||||
if data["status"] == "ok":
|
||||
connected_to_lobby.emit.call_deferred(_lobby_id)
|
||||
"join_lobby":
|
||||
if data["status"] == "ok":
|
||||
connected_to_lobby.emit.call_deferred(_lobby_id)
|
||||
|
||||
func _exit_tree() -> void:
|
||||
disconnect_from_server()
|
||||
|
||||
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()
|
||||
_stop_thread = true
|
||||
if _thread:
|
||||
_thread.wait_to_finish()
|
||||
disconnected_from_server.emit()
|
||||
|
||||
func create_lobby(client_name: String) -> void:
|
||||
_lobby_id = _generate_code()
|
||||
_client_id = _generate_code()
|
||||
_client_name = client_name
|
||||
|
||||
_send_message(TCPMessages.create_lobby(_lobby_id, _client_id, client_name))
|
||||
|
||||
func join_lobby(lobby_id: String, client_name: String) -> void:
|
||||
_lobby_id = lobby_id
|
||||
_client_id = _generate_code()
|
||||
_client_name = client_name
|
||||
|
||||
_send_message(TCPMessages.join_lobby(_lobby_id, _client_id, client_name))
|
||||
|
||||
func leave_lobby() -> void:
|
||||
_send_message(TCPMessages.leave_lobby(_lobby_id, _client_id))
|
||||
|
||||
_lobby_id = ""
|
||||
_client_id = ""
|
||||
_client_name = ""
|
||||
|
||||
func _send_message(message: String) -> void:
|
||||
_tcp.put_data(message.to_utf8_buffer())
|
||||
|
||||
func _generate_code() -> String:
|
||||
# use a local randomized RNG to keep the global RNG reproducible
|
||||
var rng = RandomNumberGenerator.new()
|
||||
rng.randomize()
|
||||
var length = 6
|
||||
var result = ''
|
||||
for _n in range(length):
|
||||
var ascii = rng.randi_range(0, 25) + 65
|
||||
result += '%c' % ascii
|
||||
return result
|
||||
Reference in New Issue
Block a user