godot-webrtc-mplayer-testing/signaller_client.gd

145 lines
4.3 KiB
GDScript3
Raw Normal View History

2021-11-15 15:02:55 -06:00
extends Node
2021-11-17 08:18:12 -06:00
"""
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.
"""
2021-11-17 13:57:45 -06:00
signal lobby_new(lobbiesList)
signal lobby_delete(id)
2021-11-17 16:24:00 -06:00
signal lobby_joined(id, peer_id)
2021-11-17 13:57:45 -06:00
signal lobby_left(id)
signal peer_joined(id)
signal peer_left(id)
2021-11-17 16:24:00 -06:00
signal candidate_received(data)
signal offer_received(data)
signal answer_received(data)
2021-12-02 16:09:43 -06:00
signal client_id_set(client_id)
signal peer_id_set(peer_id)
2021-11-17 13:57:45 -06:00
signal websocket_connected
signal websocket_disconnected
2021-11-17 08:18:12 -06:00
onready var ws: WebSocketClient = WebSocketClient.new()
2021-11-17 16:24:00 -06:00
onready var client_id = null
2021-11-17 13:57:45 -06:00
onready var lobby_id = null
2021-11-17 16:24:00 -06:00
onready var peer_id = null
2021-11-17 13:57:45 -06:00
2021-11-17 08:18:12 -06:00
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")
2021-11-15 15:02:55 -06:00
func close():
2021-11-17 08:18:12 -06:00
ws.disconnect_from_host()
func connect_to_websocket_signaller(url: String):
2021-12-02 15:13:12 -06:00
print("WebSocket: %s" % ws)
2021-11-17 08:18:12 -06:00
close()
2021-12-02 15:13:12 -06:00
print("Attempting to connect to WebSocket signalling server at %s" % url)
2021-11-17 08:18:12 -06:00
var _result = ws.connect_to_url(url)
2021-12-02 16:30:24 -06:00
print("%s" % _result)
2021-11-15 15:02:55 -06:00
2021-11-17 14:01:35 -06:00
func _closed(_unknown):
2021-11-17 13:57:45 -06:00
emit_signal("websocket_disconnected")
2021-11-15 15:02:55 -06:00
2021-11-17 08:18:12 -06:00
func _close_request(code: int, reason: String):
2021-12-02 15:13:12 -06:00
print("Received WebSocket close request from signalling server - Code: %s, Reason: %s" % [code, reason])
2021-11-15 15:02:55 -06:00
func _connected(protocol = ""):
2021-12-02 15:13:12 -06:00
print("WebSocket signaller connected via protocol %s" % protocol)
2021-11-17 08:18:12 -06:00
ws.get_peer(1).set_write_mode(WebSocketPeer.WRITE_MODE_TEXT)
2021-11-17 13:57:45 -06:00
emit_signal("websocket_connected")
2021-11-17 16:24:00 -06:00
_send("init")
2021-11-17 08:18:12 -06:00
func _process(_delta: float):
var status: int = ws.get_connection_status()
if status == WebSocketClient.CONNECTION_CONNECTING or status == WebSocketClient.CONNECTION_CONNECTED:
ws.poll()
2021-11-17 13:57:45 -06:00
func join_lobby(id: String):
return _send("lobby_join:%s" % id)
2021-11-17 08:18:12 -06:00
2021-11-17 13:57:45 -06:00
func create_lobby():
return _send("lobby_create")
2021-11-17 08:18:12 -06:00
2021-11-17 13:57:45 -06:00
func request_lobby_list():
return _send("request_lobby_list")
2021-11-17 08:18:12 -06:00
2021-11-17 13:57:45 -06:00
func request_peer_list():
return _send("request_peer_list")
2021-11-17 08:18:12 -06:00
2021-11-17 13:57:45 -06:00
func _send(s: String):
return ws.get_peer(1).put_packet(s.to_utf8())
2021-11-17 08:18:12 -06:00
2021-11-17 13:57:45 -06:00
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:
2021-12-02 15:13:12 -06:00
print("Unhandled message: %s" % msg)
2021-11-17 13:57:45 -06:00
func handle_message(data: Dictionary):
match data["type"]:
2021-12-02 16:09:43 -06:00
"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"])
2021-11-17 13:57:45 -06:00
"lobby_new": emit_signal("lobby_new", [{"id": data["id"], "name": data["name"]}])
"lobby_delete": emit_signal("lobby_delete", data["id"])
2021-12-02 16:09:43 -06:00
2021-11-17 13:57:45 -06:00
"lobby_joined":
lobby_id = data["id"]
2021-11-17 16:24:00 -06:00
peer_id = data["peerId"]
emit_signal("lobby_joined", data["id"], data["peerId"])
2021-11-17 13:57:45 -06:00
"lobby_left":
lobby_id = null
emit_signal("lobby_left", data["id"])
2021-12-02 16:09:43 -06:00
2021-11-17 16:24:00 -06:00
"peer_joined": emit_signal("peer_joined", [{"id": data["id"], "name": data["name"], "peerId": data["peerId"]}])
2021-11-17 13:57:45 -06:00
"peer_left": emit_signal("peer_left", data["id"])
2021-12-02 16:09:43 -06:00
2021-11-17 16:24:00 -06:00
"candidate":
2021-12-02 15:13:12 -06:00
print("Candidate received - Data: %s" % JSON.print(data["data"]))
2021-11-17 16:24:00 -06:00
emit_signal("candidate_received", data["data"])
"offer":
2021-12-02 15:13:12 -06:00
print("Offer received - Data: %s" % JSON.print(data["data"]))
2021-11-17 16:24:00 -06:00
emit_signal("offer_received", data["data"])
"answer":
2021-12-02 15:13:12 -06:00
print("Answer received - Data: %s" % JSON.print(data["data"]))
2021-11-17 16:24:00 -06:00
emit_signal("answer_received", data["data"])
2021-12-02 16:09:43 -06:00
2021-12-02 15:13:12 -06:00
_: print("Unhandled Message - Data: %s" % JSON.print(data))
2021-11-17 08:18:12 -06:00
2021-11-17 16:24:00 -06:00
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
}
}))
2021-11-15 15:02:55 -06:00
2021-11-17 16:24:00 -06:00
func send_offer(peerId, offer) -> int:
return _send("json:%s" % JSON.print({ "type": "offer", "data": {"peerId": peerId, "offer": offer }}))
2021-11-15 15:02:55 -06:00
2021-11-17 16:24:00 -06:00
func send_answer(peerId, answer) -> int:
return _send("json:%s" % JSON.print({ "type": "answer", "data": {"peerId": peerId, "answer": answer }}))
2021-11-15 15:02:55 -06:00
2021-11-17 16:24:00 -06:00
"""
2021-11-15 15:02:55 -06:00
elif type.begins_with("S: "):
emit_signal("lobby_sealed")
return
2021-11-17 08:18:12 -06:00
"""