1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
extends Node
class_name Network

var ws = WebSocketClient.new()
var timer = Timer.new()

signal recieved(data)
signal connected
signal err(err)

const HEADERS = {"chat": "C", "ping": "P"}


func _ready():
	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")
	ws.connect("connection_failed", self, "connectwebsocket")
	connectwebsocket()


func connectwebsocket():
	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 ", protocol)
	emit_signal("connected")


func _connection_closed(_err):
	emit_signal("err", "Connection closed")


func _connection_error():
	emit_signal("err", "Connection error")


func _data_recieved():
	var text = ws.get_peer(1).get_var()
	if text.header == HEADERS.chat:
		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()


func send_packet(variant):
	if ws.get_peer(1).is_connected_to_host() and variant.header:
		ws.get_peer(1).put_var(variant)