summaryrefslogtreecommitdiff
path: root/network
diff options
context:
space:
mode:
Diffstat (limited to 'network')
-rw-r--r--network/WebSocketTest.gd17
-rw-r--r--network/WebSocketTest.tscn12
-rw-r--r--network/websocket_client.gd76
3 files changed, 105 insertions, 0 deletions
diff --git a/network/WebSocketTest.gd b/network/WebSocketTest.gd
new file mode 100644
index 0000000..49520dd
--- /dev/null
+++ b/network/WebSocketTest.gd
@@ -0,0 +1,17 @@
+extends Node
+
+onready var client = $WebSocketClient
+var sent = false
+
+func _ready():
+ client.sock_connect_to_url("ws://127.0.0.1:8181")
+
+func _process(_delta):
+ if client.state == 2 and not sent:
+ client.send("test string".to_utf8(), true)
+ client.send(JSON.print({"items" : [1, "test_dictionary"]}).to_utf8(), true)
+ sent = true
+ if client.state == 2:
+ var message = client.receive()
+ if message:
+ print(message)
diff --git a/network/WebSocketTest.tscn b/network/WebSocketTest.tscn
new file mode 100644
index 0000000..3b02263
--- /dev/null
+++ b/network/WebSocketTest.tscn
@@ -0,0 +1,12 @@
+[gd_scene load_steps=3 format=2]
+
+[ext_resource path="res://network/WebSocketTest.gd" type="Script" id=1]
+[ext_resource path="res://network/websocket_client.gd" type="Script" id=2]
+
+[node name="WebSocketTest" type="Spatial"]
+script = ExtResource( 1 )
+
+[node name="WebSocketClient" type="Node" parent="."]
+script = ExtResource( 2 )
+
+[node name="Camera" type="Camera" parent="."]
diff --git a/network/websocket_client.gd b/network/websocket_client.gd
new file mode 100644
index 0000000..4583dc3
--- /dev/null
+++ b/network/websocket_client.gd
@@ -0,0 +1,76 @@
+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
+
+
+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 sock_connect_to_url(url):
+ print("Connecting to %s..." % url)
+ 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)
+ state = 2 # CONNECTED
+
+func on_connection_close_success(clean):
+ print("WebSocket closed successfully.")
+ socket = null
+ 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
+
+func send(message, as_string=false):
+ if state != 2: return null
+ if as_string:
+ socket.set_write_mode(WebSocketPeer.WRITE_MODE_TEXT)
+ return socket.put_packet(message)
+ else:
+ socket.set_write_mode(WebSocketPeer.WRITE_MODE_BINARY)
+ return socket.put_packet(var2bytes(message))
+
+func send_json(message) -> int:
+ if state != 2: return -1
+ var message_json = JSON.print(message).to_utf8()
+ socket.set_write_mode(WebSocketPeer.WRITE_MODE_TEXT)
+ 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
+ print("receive")
+ var packet : PoolByteArray = socket.get_packet()
+ if socket.was_string_packet():
+ var message = packet.get_string_from_utf8()
+ if string_to_json: message = JSON.parse(message)
+ return message
+ return bytes2var(packet)
+
+
+func _process(_delta):
+ socket_client.poll()