server: added SSL context for running on remote server
This commit is contained in:
@@ -2,7 +2,6 @@ class_name ClientInterface
|
||||
extends Control
|
||||
|
||||
@export var client: WebClientTCP
|
||||
@export var game_client: GameClient
|
||||
|
||||
@export var connect_button: Button
|
||||
@export var create_lobby_button: Button
|
||||
@@ -22,14 +21,12 @@ func _write_line(text: String) -> void:
|
||||
|
||||
func _on_create_lobby_button_pressed() -> void:
|
||||
client.create_lobby(name_input.text.strip_edges())
|
||||
game_client.is_host = true
|
||||
|
||||
func _on_connect_button_pressed() -> void:
|
||||
client.connect_to_server()
|
||||
client.connect_to_server($MarginContainer/VBoxContainer/URLInput.text)
|
||||
|
||||
func _on_join_lobby_button_pressed() -> void:
|
||||
client.join_lobby(lobby_code_input.text.strip_edges(), name_input.text.strip_edges())
|
||||
game_client.is_host = false
|
||||
|
||||
func _on_disconnect_lobby_button_pressed() -> void:
|
||||
leave_lobby_button.disabled = true
|
||||
|
||||
@@ -17,6 +17,8 @@ var cached_response: String
|
||||
var _thread: Thread
|
||||
var _stop_thread: bool = false
|
||||
|
||||
var _players_in_lobby: int = 0
|
||||
|
||||
signal connected_to_server()
|
||||
signal connection_error_occurred()
|
||||
signal disconnected_from_server()
|
||||
@@ -47,8 +49,8 @@ func _process(_delta: float) -> void:
|
||||
WebSocketPeer.STATE_OPEN:
|
||||
print("open")
|
||||
_stop_thread = false
|
||||
_thread = Thread.new()
|
||||
_thread.start(_listen_for_data)
|
||||
#_thread = Thread.new()
|
||||
#_thread.start(_listen_for_data)
|
||||
connected_to_server.emit()
|
||||
connection_status_changed.emit(ConnectionStatus.LOBBYLESS)
|
||||
WebSocketPeer.STATE_CONNECTING:
|
||||
@@ -59,40 +61,13 @@ func _process(_delta: float) -> void:
|
||||
pass
|
||||
WebSocketPeer.STATE_CLOSED:
|
||||
print("closed")
|
||||
_clean_up_thread()
|
||||
#_clean_up_thread()
|
||||
connection_status_changed.emit(ConnectionStatus.DISCONNECTED)
|
||||
|
||||
_processed_status = _new_status
|
||||
#_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:
|
||||
#_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()
|
||||
#connection_status_changed.emit(ConnectionStatus.LOBBYLESS)
|
||||
#StreamPeerTCP.Status.STATUS_ERROR:
|
||||
#connection_error_occurred.emit()
|
||||
#connection_status_changed.emit(ConnectionStatus.DISCONNECTED)
|
||||
#
|
||||
#_processed_status = _new_status
|
||||
|
||||
func _listen_for_data() -> void:
|
||||
while true:
|
||||
#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
|
||||
if _socket.get_available_packet_count():
|
||||
|
||||
if _processed_status == WebSocketPeer.STATE_OPEN:
|
||||
while _socket.get_available_packet_count():
|
||||
cached_response = _socket.get_packet().get_string_from_utf8()
|
||||
print(cached_response)
|
||||
#cached_response += (received_data[1] as PackedByteArray).get_string_from_utf8()
|
||||
@@ -101,6 +76,15 @@ func _listen_for_data() -> void:
|
||||
_handle_response(cached_response)
|
||||
cached_response = ""
|
||||
|
||||
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 = ""
|
||||
|
||||
func _handle_response(response: String) -> void:
|
||||
var data = JSON.parse_string(response)
|
||||
match data["response"]:
|
||||
@@ -113,6 +97,7 @@ func _handle_response(response: String) -> void:
|
||||
"join_lobby":
|
||||
if data["status"] == "ok":
|
||||
_client_id = data["client_id"]
|
||||
_players_in_lobby = data["connected_player_count"]
|
||||
connected_to_lobby.emit.call_deferred(_lobby_id)
|
||||
connection_status_changed.emit.call_deferred(ConnectionStatus.JOINED)
|
||||
"client_joined_lobby":
|
||||
@@ -121,6 +106,7 @@ func _handle_response(response: String) -> void:
|
||||
leave_lobby()
|
||||
"leave_lobby":
|
||||
if data["status"] == "ok":
|
||||
_players_in_lobby = data["connected_player_count"]
|
||||
client_left_lobby.emit.call_deferred(data["client_name"])
|
||||
"client_left_lobby":
|
||||
client_left_lobby.emit.call_deferred(data["client_name"])
|
||||
@@ -130,26 +116,16 @@ func _handle_response(response: String) -> void:
|
||||
func _exit_tree() -> void:
|
||||
disconnect_from_server()
|
||||
|
||||
func connect_to_server() -> void:
|
||||
#_tcp.connect_to_host("168.119.97.227", 9974)
|
||||
#_tcp.set_no_delay(true)
|
||||
var err = _socket.connect_to_url("127.0.0.1:9974")
|
||||
func connect_to_server(text: String) -> void:
|
||||
var err = _socket.connect_to_url(text)
|
||||
if err != OK:
|
||||
print("didn't work!")
|
||||
|
||||
func disconnect_from_server() -> void:
|
||||
#_tcp.disconnect_from_host()
|
||||
_socket.close()
|
||||
_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()
|
||||
_thread = null
|
||||
|
||||
func create_lobby(client_name: String) -> void:
|
||||
_client_name = client_name
|
||||
|
||||
@@ -175,5 +151,4 @@ func send_chat_message(message: String) -> void:
|
||||
_send_message(TCPMessages.send_chat_message(_lobby_id, _client_id, message))
|
||||
|
||||
func _send_message(message: String) -> void:
|
||||
#_tcp.put_data(message.to_utf8_buffer())
|
||||
_socket.send_text(message)
|
||||
|
||||
Reference in New Issue
Block a user