hexland/Scripts/WebsocketController.gd

39 lines
826 B
GDScript3
Raw Normal View History

extends Node
var client = WebSocketClient.new()
var peer
signal hostjoin
func _init():
client.connect("connection_established", self, "_on_connect")
client.connect("data_received", self, "_on_data")
func _on_connect(_proto):
peer = client.get_peer(1)
peer.set_write_mode(WebSocketPeer.WRITE_MODE_TEXT)
func _on_data():
var msg = peer.get_packet().get_string_from_utf8()
var regex = RegEx.new()
regex.compile("([\\w_]+):")
var res = regex.search(msg)
match res.strings[1]:
"HOSTJOIN":
emit_signal("hostjoin")
func _process(_delta):
client.poll()
func send_message(message):
peer.put_packet(message.to_utf8())
func connect_websocket():
client.connect_to_url(ProjectSettings["global/server_url"]);
func host_game():
send_message("HOST:")
func join_game(hash_):
send_message("JOIN: %s" % hash_)