extends Node """ This module is responsible for making a WebSocket connection to the signaller in order to enable establishish WebRTC P2P connections. Another module is expected to fully setup the peer connections. """ signal lobby_new(lobbiesList) signal lobby_delete(id) signal lobby_joined(id, peer_id) signal lobby_left(id) signal peer_joined(id) signal peer_left(id) signal candidate_received(data) signal offer_received(data) signal answer_received(data) signal client_id_set(client_id) signal peer_id_set(peer_id) signal websocket_connected signal websocket_disconnected onready var ws: WebSocketClient = WebSocketClient.new() onready var client_id = null onready var lobby_id = null onready var peer_id = null func _ready(): var _result = ws.connect("data_received", self, "_parse_msg") _result = ws.connect("connection_established", self, "_connected") _result = ws.connect("connection_closed", self, "_closed") _result = ws.connect("connection_error", self, "_closed") _result = ws.connect("server_close_request", self, "_close_request") func close(): ws.disconnect_from_host() func connect_to_websocket_signaller(url: String): print("WebSocket: %s" % ws) close() print("Attempting to connect to WebSocket signalling server at %s" % url) var _result = ws.connect_to_url(url) print("%s" % _result) func _closed(_unknown): emit_signal("websocket_disconnected") func _close_request(code: int, reason: String): print("Received WebSocket close request from signalling server - Code: %s, Reason: %s" % [code, reason]) func _connected(protocol = ""): print("WebSocket signaller connected via protocol %s" % protocol) ws.get_peer(1).set_write_mode(WebSocketPeer.WRITE_MODE_TEXT) emit_signal("websocket_connected") _send("init") func _process(_delta: float): var status: int = ws.get_connection_status() if status == WebSocketClient.CONNECTION_CONNECTING or status == WebSocketClient.CONNECTION_CONNECTED: ws.poll() func join_lobby(id: String): return _send("lobby_join:%s" % id) func create_lobby(): return _send("lobby_create") func request_lobby_list(): return _send("request_lobby_list") func request_peer_list(): return _send("request_peer_list") func _send(s: String): return ws.get_peer(1).put_packet(s.to_utf8()) func _parse_msg(): var msg: String = ws.get_peer(1).get_packet().get_string_from_utf8() if msg.begins_with("json:"): var data = JSON.parse(msg.substr(5)) if data.error == OK: handle_message(data.result) else: print("Unhandled message: %s" % msg) func handle_message(data: Dictionary): match data["type"]: "your_id": client_id = data["data"] emit_signal("client_id_set", client_id) "your_peer_id": peer_id = int(data["data"]) emit_signal("peer_id_set", peer_id) "lobby_list": emit_signal("lobby_new", data["data"]) "lobby_new": emit_signal("lobby_new", [{"id": data["id"], "name": data["name"]}]) "lobby_delete": emit_signal("lobby_delete", data["id"]) "lobby_joined": lobby_id = data["id"] peer_id = data["peerId"] emit_signal("lobby_joined", data["id"], data["peerId"]) "lobby_left": lobby_id = null emit_signal("lobby_left", data["id"]) "peer_joined": emit_signal("peer_joined", [{"id": data["id"], "name": data["name"], "peerId": data["peerId"]}]) "peer_left": emit_signal("peer_left", data["id"]) "candidate": print("Candidate received - Data: %s" % JSON.print(data["data"])) emit_signal("candidate_received", data["data"]) "offer": print("Offer received - Data: %s" % JSON.print(data["data"])) emit_signal("offer_received", data["data"]) "answer": print("Answer received - Data: %s" % JSON.print(data["data"])) emit_signal("answer_received", data["data"]) _: print("Unhandled Message - Data: %s" % JSON.print(data)) func send_candidate(peerId, mid, index, sdp) -> int: return _send("json:%s" % JSON.print({ "type": "candidate", "data": { "peerId": peerId, "mid": mid, "index": index, "sdp": sdp } })) func send_offer(peerId, offer) -> int: return _send("json:%s" % JSON.print({ "type": "offer", "data": {"peerId": peerId, "offer": offer }})) func send_answer(peerId, answer) -> int: return _send("json:%s" % JSON.print({ "type": "answer", "data": {"peerId": peerId, "answer": answer }})) """ elif type.begins_with("S: "): emit_signal("lobby_sealed") return """