137 lines
4.3 KiB
GDScript
137 lines
4.3 KiB
GDScript
extends Node
|
|
|
|
"""
|
|
This module sets up WebRTC peer connections.
|
|
"""
|
|
|
|
signal peer_connected(id)
|
|
signal peer_disconnected(id)
|
|
signal connection_succeeded()
|
|
signal signaller_disconnected()
|
|
|
|
onready var webrtc = WebRTCMultiplayer.new()
|
|
|
|
var signaller_client = null
|
|
var ice_servers = null
|
|
var peers = {}
|
|
var peers_id_mappings = {}
|
|
|
|
func _init(_ice_servers, _signaller_client):
|
|
ice_servers = _ice_servers
|
|
signaller_client = _signaller_client
|
|
print("Initializing WebRTCNegotiator with ICE servers: %s" % ice_servers)
|
|
|
|
func _ready():
|
|
signaller_client.connect("disconnected", self, "_signaller_disconnected")
|
|
|
|
signaller_client.connect("offer_received", self, "_offer_received")
|
|
signaller_client.connect("answer_received", self, "_answer_received")
|
|
signaller_client.connect("candidate_received", self, "_candidate_received")
|
|
|
|
signaller_client.connect("peer_data", self, "_peer_data")
|
|
|
|
webrtc.connect("connection_succeeded", self, "_connection_succeeded")
|
|
webrtc.connect("peer_connected", self, "_peer_connected")
|
|
webrtc.connect("peer_disconnected", self, "_peer_disconnected")
|
|
|
|
# connect("lobby_sealed", self, "lobby_sealed")
|
|
|
|
add_child(self.signaller_client)
|
|
|
|
func _peer_connected(peer_id):
|
|
print("FULLY CONNECTED PEER: %s" % peer_id)
|
|
emit_signal("peer_connected", peer_id)
|
|
|
|
func _peer_disconnected(peer_id):
|
|
print("FULLY CONNECTED PEER DISCONNECTING: %s" % peer_id)
|
|
emit_signal("peer_disconnected")
|
|
|
|
func _connection_succeeded():
|
|
print("FULLY CONNECTED")
|
|
emit_signal("connection_succeeded")
|
|
|
|
func _signaller_disconnected():
|
|
print("SIGNALLER DISCONNECTED")
|
|
emit_signal("signaller_disconnected")
|
|
|
|
func close():
|
|
webrtc.close()
|
|
signaller_client.close()
|
|
|
|
func connect_to_signaller():
|
|
signaller_client.connect_websocket()
|
|
|
|
func peer_by_peer_id(peer_id):
|
|
if peers_id_mappings.has(peer_id):
|
|
var p = peers_id_mappings[peer_id]
|
|
if peers.has(p):
|
|
return peers[p]
|
|
return null
|
|
|
|
func _create_peer(peer_id):
|
|
# if peer_id == signaller_client.peer_id: return
|
|
var peer: WebRTCPeerConnection = WebRTCPeerConnection.new()
|
|
print("New Net Peer with peer_id %d" % [peer_id])
|
|
var _peer_init_results = peer.initialize({"iceServers": ice_servers})
|
|
var _result = peer.connect("session_description_created", self, "_offer_created", [peer_id])
|
|
_result = peer.connect("ice_candidate_created", self, "_new_ice_candidate", [peer_id])
|
|
webrtc.add_peer(peer, peer_id)
|
|
var uid = webrtc.get_unique_id()
|
|
if peer_id > uid:
|
|
var offer_result = peer.create_offer()
|
|
print("Create Offer Result: %s" % offer_result)
|
|
else:
|
|
print("Did not create offer: %d <= %d" % [peer_id, uid])
|
|
return peer
|
|
|
|
func _new_ice_candidate(mid, index, sdp, peer_id):
|
|
print("New ICE Candidate - MID: %s, Index: %s, SDP: %s, PeerID: %s" % [mid, index, sdp, peer_id])
|
|
signaller_client.send_candidate(peer_id, mid, index, sdp)
|
|
|
|
func _offer_created(type, data, peer_id):
|
|
print("%s created - PeerID: %s, Data: %s" % [type, peer_id, JSON.print(data)])
|
|
if not webrtc.has_peer(peer_id): return
|
|
webrtc.get_peer(peer_id).connection.set_local_description(type, data)
|
|
if type == "offer": signaller_client.send_offer(peer_id, data)
|
|
else: signaller_client.send_answer(peer_id, data)
|
|
|
|
func _peer_id_set(peer_id):
|
|
print("Peer ID received %s - setting up to peer..." % [peer_id])
|
|
webrtc.initialize(peer_id, true)
|
|
get_tree().network_peer = webrtc
|
|
|
|
func _peer_data(peers):
|
|
for i in range(len(peers)):
|
|
var peer = peers[i]
|
|
if peer.has("peerId"):
|
|
var peer_id = peer.peerId
|
|
if not webrtc.has_peer(peer_id):
|
|
_create_peer(peer_id)
|
|
|
|
func peer_disconnected(id):
|
|
print("Peer %s disconnected" % id)
|
|
if webrtc.has_peer(id):
|
|
webrtc.remove_peer(id)
|
|
|
|
func _offer_received(data):
|
|
var id = data.peerId
|
|
if webrtc.has_peer(id):
|
|
print("Setting offer remote description: %s" % JSON.print(data.offer))
|
|
webrtc.get_peer(id).connection.set_remote_description("offer", data.offer)
|
|
else:
|
|
print("Received an offer for a peer with ID %s that hasn't been added" % id)
|
|
|
|
func _answer_received(data):
|
|
var id = data.peerId
|
|
if webrtc.has_peer(id):
|
|
print("Setting answer remote description: %s" % JSON.print(data))
|
|
webrtc.get_peer(id).connection.set_remote_description("answer", data.answer)
|
|
|
|
func _candidate_received(data):
|
|
var id = data.peerId
|
|
if webrtc.has_peer(id):
|
|
print("Adding ice candidate: %s" % JSON.print(data))
|
|
webrtc.get_peer(id).connection.add_ice_candidate(data.mid, data.index, data.sdp)
|
|
else:
|
|
print("Received candidate for non-existant peer %s" % id)
|