Diffstat (limited to 'WS.gd')
-rw-r--r--WS.gd30
1 files changed, 20 insertions, 10 deletions
diff --git a/WS.gd b/WS.gd
index 5559493..e04bf78 100644
--- a/WS.gd
+++ b/WS.gd
@@ -1,23 +1,32 @@
extends Node
+class_name Network
-var ws = null
+var ws = WebSocketClient.new()
+var timer = Timer.new()
signal recieved(data)
func _ready():
- ws = WebSocketClient.new()
+ add_child(timer)
+ timer.start(5)
+ timer.connect("timeout", self, "ping")
ws.connect("connection_established", self, "_connection_established")
ws.connect("connection_closed", self, "_connection_closed")
ws.connect("connection_error", self, "_connection_error")
+ ws.connect("data_received", self, "_data_recieved")
var url = "https://chat-server-gd.herokuapp.com/"
print("Connecting to " + url)
ws.connect_to_url(url)
+func ping():
+ send_packet({"header": "P"})
+
+
func _connection_established(protocol):
- print("Connection established with protocol: ", protocol)
+ print("Connection established ", protocol)
func _connection_closed(_err):
@@ -28,19 +37,20 @@ func _connection_error():
print("Connection error")
+func _data_recieved():
+ var text = ws.get_peer(1).get_var()
+ emit_signal("recieved", text)
+ print("recieved %s" % text.text)
+
+
func _process(_delta):
if (
ws.get_connection_status() == ws.CONNECTION_CONNECTING
|| ws.get_connection_status() == ws.CONNECTION_CONNECTED
):
ws.poll()
- if ws.get_peer(1).is_connected_to_host():
- if ws.get_peer(1).get_available_packet_count() > 0:
- var text = ws.get_peer(1).get_var()
- emit_signal("recieved", text.value)
- print("recieved %s" % text.value)
-func send_packet(variant: String):
- if ws.get_peer(1).is_connected_to_host():
+func send_packet(variant):
+ if ws.get_peer(1).is_connected_to_host() and variant.header:
ws.get_peer(1).put_var(variant)