client & server: changed from TCP stream to WebSocket implementations
This commit is contained in:
@@ -1,9 +1,10 @@
|
||||
class_name WebClientTCP
|
||||
extends Node
|
||||
|
||||
var _tcp := StreamPeerTCP.new()
|
||||
#var _tcp := StreamPeerTCP.new()
|
||||
var _socket := WebSocketPeer.new()
|
||||
|
||||
var _new_status: int = -1
|
||||
var _new_status: int = WebSocketPeer.STATE_CLOSED
|
||||
var _processed_status: int = -1
|
||||
|
||||
var _lobby_id: String
|
||||
@@ -39,42 +40,66 @@ enum ConnectionStatus {
|
||||
}
|
||||
|
||||
func _process(_delta: float) -> void:
|
||||
_tcp.poll()
|
||||
_new_status = _tcp.get_status()
|
||||
# Ensure status change is only processed once
|
||||
_socket.poll()
|
||||
_new_status = _socket.get_ready_state()
|
||||
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:
|
||||
WebSocketPeer.STATE_OPEN:
|
||||
print("open")
|
||||
_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()
|
||||
WebSocketPeer.STATE_CONNECTING:
|
||||
print("connecting")
|
||||
connection_status_changed.emit(ConnectionStatus.ATTEMPTING_CONNECTION)
|
||||
WebSocketPeer.STATE_CLOSING:
|
||||
print("closing")
|
||||
pass
|
||||
WebSocketPeer.STATE_CLOSED:
|
||||
print("closed")
|
||||
_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 not _stop_thread:
|
||||
received_data = _tcp.get_data(1)
|
||||
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
|
||||
|
||||
cached_response += (received_data[1] as PackedByteArray).get_string_from_utf8()
|
||||
if cached_response.ends_with("}"):
|
||||
#if not received_data[0] == OK:
|
||||
#disconnected_from_server.emit.call_deferred()
|
||||
#return
|
||||
if _socket.get_available_packet_count():
|
||||
cached_response = _socket.get_packet().get_string_from_utf8()
|
||||
print(cached_response)
|
||||
_handle_response(cached_response)
|
||||
cached_response = ""
|
||||
#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)
|
||||
@@ -106,11 +131,15 @@ 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)
|
||||
#_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")
|
||||
if err != OK:
|
||||
print("didn't work!")
|
||||
|
||||
func disconnect_from_server() -> void:
|
||||
_tcp.disconnect_from_host()
|
||||
#_tcp.disconnect_from_host()
|
||||
_socket.close()
|
||||
_clean_up_thread()
|
||||
disconnected_from_server.emit()
|
||||
connection_status_changed.emit(ConnectionStatus.DISCONNECTED)
|
||||
@@ -146,4 +175,5 @@ 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())
|
||||
#_tcp.put_data(message.to_utf8_buffer())
|
||||
_socket.send_text(message)
|
||||
|
||||
Reference in New Issue
Block a user