summaryrefslogtreecommitdiff
path: root/network
diff options
context:
space:
mode:
Diffstat (limited to 'network')
-rw-r--r--network/GCClient.tscn6
-rw-r--r--network/GameCoordinatorClient.gd101
-rw-r--r--network/WSClient.tscn6
-rw-r--r--network/websocket_client_basic.gd (renamed from network/websocket_client.gd)1
4 files changed, 107 insertions, 7 deletions
diff --git a/network/GCClient.tscn b/network/GCClient.tscn
new file mode 100644
index 0000000..2b04171
--- /dev/null
+++ b/network/GCClient.tscn
@@ -0,0 +1,6 @@
+[gd_scene load_steps=2 format=2]
+
+[ext_resource path="res://network/GameCoordinatorClient.gd" type="Script" id=1]
+
+[node name="GCClient" type="Node"]
+script = ExtResource( 1 )
diff --git a/network/GameCoordinatorClient.gd b/network/GameCoordinatorClient.gd
new file mode 100644
index 0000000..3d87b7b
--- /dev/null
+++ b/network/GameCoordinatorClient.gd
@@ -0,0 +1,101 @@
+extends Node
+
+# CONNECTION DATA
+enum { NONE, SERVER_BROWSER, LOBBY, VOICE }
+var socket_client: WebSocketClient = null
+var socket: WebSocketPeer = null
+var state: int = 0 # -1 = CONNECTION_FAILED, 0 = CONNECTION_DISCONNECTED, 1 = CONNECTION_CONNECTING, 2 = CONNECTION_CONNECTED, 3 = CONNECTION_DISCONNECTING
+var message_queue : Array = [] # messages to be sent upon connection
+var connection_type : int = NONE
+
+# GC DATA
+var is_host : bool
+var host_id : String
+var lobby_id : String
+var player_id : String
+var rejoin_key : String
+var players : Array = []
+
+func _ready():
+ socket_client = WebSocketClient.new()
+ socket_client.connect("connection_established", self, "on_connection_success")
+ socket_client.connect("connection_closed", self, "on_connection_close_success")
+ socket_client.connect("connection_error", self, "on_connection_error")
+ #socket_client.connect("data_received", self, "receive")
+
+func connect_to_gc(c_type: int):
+ if !(c_type in [SERVER_BROWSER, LOBBY, VOICE]):
+ print("Invalid connection type!")
+ return
+ connection_type = c_type
+ var url : String = Globals.GC_URL
+ print("Connecting to game coordinator: %s..." % url)
+ message_queue.clear()
+ var error = socket_client.connect_to_url(url)
+ if error != OK:
+ return error
+
+ state = 1 # CONNECTING
+ return OK
+
+func sock_close(code = 1000, reason = ""):
+ print("Closing websocket...")
+ socket_client.disconnect_from_host(code, reason)
+ state = 3 # DISCONNECTING
+
+func on_connection_success(protocol):
+ print("WebSocket connection success with protocol %s." % protocol)
+ socket = socket_client.get_peer(1)
+ socket.set_write_mode(WebSocketPeer.WRITE_MODE_TEXT) # defaults to text mode
+ state = 2 # CONNECTED
+
+ while len(message_queue) > 0:
+ var msg = message_queue.pop_at(0)
+ send(msg)
+
+func on_connection_close_success(clean):
+ print("WebSocket closed successfully.")
+ socket = null
+ connection_type = NONE
+ if clean:
+ state = 0 # DISCONNECTED
+ else:
+ state = -1 # DISCONNECT DIRTY
+
+func on_connection_error(): # connection failed
+ print("WebSocket connection failed!")
+ socket = null
+ state = -1 # DISCONNECT DIRTY
+ connection_type = NONE
+
+func send(message, as_bytes=false) -> int:
+ if state != 2:
+ message_queue.push_back(message)
+ return -1
+ return socket.put_packet(message)
+
+
+func send_json(message) -> int:
+ if state != 2:
+ message_queue.push_back(JSON.print(message).to_utf8())
+ return -1
+ var message_json = JSON.print(message).to_utf8()
+ return socket.put_packet(message_json)
+
+func receive(string_to_json=false):
+ if state != 2: return null
+ if socket.get_available_packet_count() < 1: return null
+ var packet : PoolByteArray = socket.get_packet()
+ if socket.was_string_packet():
+ var message = packet.get_string_from_utf8()
+ if string_to_json:
+ var json = JSON.parse(message)
+ if json.error:
+ return null
+ message = json.result
+ return message
+ return bytes2var(packet)
+
+
+func _process(_delta):
+ socket_client.poll()
diff --git a/network/WSClient.tscn b/network/WSClient.tscn
deleted file mode 100644
index 2224ed5..0000000
--- a/network/WSClient.tscn
+++ /dev/null
@@ -1,6 +0,0 @@
-[gd_scene load_steps=2 format=2]
-
-[ext_resource path="res://network/websocket_client.gd" type="Script" id=1]
-
-[node name="WSClient" type="Node"]
-script = ExtResource( 1 )
diff --git a/network/websocket_client.gd b/network/websocket_client_basic.gd
index d10f518..353dd81 100644
--- a/network/websocket_client.gd
+++ b/network/websocket_client_basic.gd
@@ -3,7 +3,6 @@ extends Node
var socket_client: WebSocketClient = null
var socket: WebSocketPeer = null
var state: int = 0 # -1 = CONNECTION_FAILED, 0 = CONNECTION_DISCONNECTED, 1 = CONNECTION_CONNECTING, 2 = CONNECTION_CONNECTED, 3 = CONNECTION_DISCONNECTING
-var id
var message_queue : Array = [] # messages to be sent upon connection