107 lines
3.5 KiB
GDScript
107 lines
3.5 KiB
GDScript
extends Node
|
|
|
|
"""
|
|
This module sets up WebRTC peer connections.
|
|
"""
|
|
|
|
var multiplayer_url = "wss://webrtc-signaller.deno.dev" # "ws://localhost:8888"
|
|
var webrtc_ice_servers = [
|
|
{ "urls": ["stun:stun.l.google.com:19302"] },
|
|
# { "urls": ["stun:localhost:3478"] }
|
|
]
|
|
|
|
const SignallerClient = preload("signaller_client.gd")
|
|
|
|
onready var mp = WebRTCMultiplayer.new()
|
|
onready var signaller = SignallerClient.new()
|
|
|
|
func _ready():
|
|
# signaller.connect("websocket_connected", self, "connected")
|
|
# signaller.connect("websocket_disconnected", self, "disconnected")
|
|
|
|
signaller.connect("offer_received", self, "offer_received")
|
|
signaller.connect("answer_received", self, "answer_received")
|
|
signaller.connect("candidate_received", self, "candidate_received")
|
|
|
|
signaller.connect("peer_id_set", self, "_peer_id_set")
|
|
# connect("lobby_sealed", self, "lobby_sealed")
|
|
signaller.connect("peer_joined", self, "_peer_joined")
|
|
# connect("peer_disconnected", self, "peer_disconnected")
|
|
add_child(signaller)
|
|
|
|
func close():
|
|
mp.close()
|
|
signaller.close()
|
|
|
|
func connect_to_signaller():
|
|
close()
|
|
signaller.connect_to_websocket_signaller(multiplayer_url)
|
|
|
|
func _create_peer(peer_id):
|
|
# if peer_id == signaller.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": webrtc_ice_servers})
|
|
peer.connect("session_description_created", self, "_offer_created", [peer_id])
|
|
peer.connect("ice_candidate_created", self, "_new_ice_candidate", [peer_id])
|
|
mp.add_peer(peer, peer_id)
|
|
var uid = mp.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_name, index_name, sdp_name, peer_id):
|
|
print("New ICE Candidate - MID: %s, Index: %s, SDP: %s, PeerID: %s" % [mid_name, index_name, sdp_name, peer_id])
|
|
signaller.send_candidate(peer_id, mid_name, index_name, sdp_name)
|
|
|
|
func _offer_created(type, data, peer_id):
|
|
print("Offer created - Type: %s, PeerID: %s, Data: %s" % [type, peer_id, JSON.print(data)])
|
|
if not mp.has_peer(peer_id):
|
|
return
|
|
mp.get_peer(peer_id).connection.set_local_description(type, data)
|
|
if type == "offer":
|
|
signaller.send_offer(peer_id, data)
|
|
else:
|
|
signaller.send_answer(peer_id, data)
|
|
|
|
func _peer_id_set(peer_id):
|
|
print("Peer ID received %s - setting up to peer..." % [peer_id])
|
|
mp.initialize(peer_id, true)
|
|
get_tree().network_peer = mp
|
|
|
|
func _peer_joined(peers):
|
|
for i in range(len(peers)):
|
|
var peer_id = peers[i]["peerId"]
|
|
if not mp.has_peer(peer_id):
|
|
_create_peer(peer_id)
|
|
|
|
func peer_disconnected(id):
|
|
print("Peer %s disconnected" % id)
|
|
if mp.has_peer(id):
|
|
mp.remove_peer(id)
|
|
|
|
func offer_received(data):
|
|
var id = data["peerId"]
|
|
if mp.has_peer(id):
|
|
print("Setting offer remote description: %s" % JSON.print(data))
|
|
mp.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 mp.has_peer(id):
|
|
print("Setting answer remote description: %s" % JSON.print(data))
|
|
mp.get_peer(id).connection.set_remote_description("answer", data["answer"])
|
|
|
|
func candidate_received(data):
|
|
var id = data["peerId"]
|
|
if mp.has_peer(id):
|
|
print("Adding ice candidate: %s" % JSON.print(data))
|
|
mp.get_peer(id).connection.add_ice_candidate(data["mid"], data["index"], data["sdp"])
|
|
else:
|
|
print("Received candidate for non-existant peer %s" % id)
|