2021-11-17 08:18:12 -06:00
|
|
|
extends Node
|
2021-11-15 15:02:55 -06:00
|
|
|
|
2021-11-17 08:18:12 -06:00
|
|
|
"""
|
|
|
|
This module sets up WebRTC peer connections.
|
|
|
|
"""
|
2021-11-15 15:02:55 -06:00
|
|
|
|
2021-11-17 08:18:12 -06:00
|
|
|
var multiplayer_url = "ws://localhost:8888"
|
|
|
|
var webrtc_ice_servers = [
|
|
|
|
{ "urls": ["stun:stun.l.google.com:19302"] }
|
|
|
|
]
|
2021-11-15 15:02:55 -06:00
|
|
|
|
2021-11-17 08:18:12 -06:00
|
|
|
const SignallerClient = preload("signaller_client.gd")
|
2021-11-15 15:02:55 -06:00
|
|
|
|
2021-11-17 08:18:12 -06:00
|
|
|
onready var mp = WebRTCMultiplayer.new()
|
2021-11-17 13:57:45 -06:00
|
|
|
onready var signaller = SignallerClient.new()
|
2021-11-15 15:02:55 -06:00
|
|
|
|
2021-11-17 08:18:12 -06:00
|
|
|
func _ready():
|
|
|
|
# connect("connected", self, "connected")
|
|
|
|
# connect("disconnected", self, "disconnected")
|
2021-11-15 15:02:55 -06:00
|
|
|
|
2021-11-17 08:18:12 -06:00
|
|
|
# connect("offer_received", self, "offer_received")
|
|
|
|
# connect("answer_received", self, "answer_received")
|
|
|
|
# connect("candidate_received", self, "candidate_received")
|
|
|
|
|
|
|
|
# connect("lobby_joined", self, "lobby_joined")
|
|
|
|
# connect("lobby_sealed", self, "lobby_sealed")
|
|
|
|
# connect("peer_connected", self, "peer_connected")
|
|
|
|
# connect("peer_disconnected", self, "peer_disconnected")
|
2021-11-17 13:57:45 -06:00
|
|
|
add_child(signaller)
|
2021-11-17 08:18:12 -06:00
|
|
|
|
|
|
|
func close():
|
|
|
|
mp.close()
|
2021-11-17 13:57:45 -06:00
|
|
|
signaller.close()
|
2021-11-17 08:18:12 -06:00
|
|
|
|
|
|
|
func connect_to_signaller():
|
2021-11-15 15:02:55 -06:00
|
|
|
close()
|
2021-11-17 13:57:45 -06:00
|
|
|
signaller.connect_to_websocket_signaller(multiplayer_url)
|
2021-11-15 15:02:55 -06:00
|
|
|
|
|
|
|
func _create_peer(id):
|
|
|
|
var peer: WebRTCPeerConnection = WebRTCPeerConnection.new()
|
2021-11-17 08:18:12 -06:00
|
|
|
peer.initialize({"iceServers": webrtc_ice_servers})
|
2021-11-15 15:02:55 -06:00
|
|
|
peer.connect("session_description_created", self, "_offer_created", [id])
|
|
|
|
peer.connect("ice_candidate_created", self, "_new_ice_candidate", [id])
|
2021-11-17 08:18:12 -06:00
|
|
|
mp.add_peer(peer, id)
|
|
|
|
if id > mp.get_unique_id():
|
|
|
|
# TODO: peer.create_offer()
|
|
|
|
pass
|
2021-11-15 15:02:55 -06:00
|
|
|
return peer
|
|
|
|
|
|
|
|
func _new_ice_candidate(mid_name, index_name, sdp_name, id):
|
2021-11-15 15:43:33 -06:00
|
|
|
print("New ICE Candidate: ", mid_name, index_name, sdp_name, id)
|
2021-11-17 08:18:12 -06:00
|
|
|
# TODO: send_candidate(id, mid_name, index_name, sdp_name)
|
2021-11-15 15:02:55 -06:00
|
|
|
|
|
|
|
func _offer_created(type, data, id):
|
2021-11-17 08:18:12 -06:00
|
|
|
if not mp.has_peer(id):
|
2021-11-15 15:02:55 -06:00
|
|
|
return
|
|
|
|
print("created", type)
|
2021-11-17 08:18:12 -06:00
|
|
|
mp.get_peer(id).connection.set_local_description(type, data)
|
|
|
|
if type == "offer":
|
|
|
|
# TODO: send_offer(id, data)
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
# TODO: send_answer(id, data)
|
|
|
|
pass
|
2021-11-15 15:02:55 -06:00
|
|
|
|
|
|
|
func connected(id):
|
|
|
|
print("Connected %d" % id)
|
2021-11-17 08:18:12 -06:00
|
|
|
mp.initialize(id, true)
|
2021-11-15 15:02:55 -06:00
|
|
|
|
|
|
|
func peer_connected(id):
|
|
|
|
print("Peer connected %d" % id)
|
|
|
|
_create_peer(id)
|
|
|
|
|
|
|
|
func peer_disconnected(id):
|
2021-11-17 08:18:12 -06:00
|
|
|
if mp.has_peer(id):
|
|
|
|
mp.remove_peer(id)
|
2021-11-15 15:02:55 -06:00
|
|
|
|
|
|
|
func offer_received(id, offer):
|
|
|
|
print("Got offer: %d" % id)
|
2021-11-17 08:18:12 -06:00
|
|
|
if mp.has_peer(id):
|
|
|
|
mp.get_peer(id).connection.set_remote_description("offer", offer)
|
2021-11-15 15:02:55 -06:00
|
|
|
|
|
|
|
func answer_received(id, answer):
|
|
|
|
print("Got answer: %d" % id)
|
2021-11-17 08:18:12 -06:00
|
|
|
if mp.has_peer(id):
|
|
|
|
mp.get_peer(id).connection.set_remote_description("answer", answer)
|
2021-11-15 15:02:55 -06:00
|
|
|
|
|
|
|
func candidate_received(id, mid, index, sdp):
|
2021-11-17 08:18:12 -06:00
|
|
|
if mp.has_peer(id):
|
|
|
|
mp.get_peer(id).connection.add_ice_candidate(mid, index, sdp)
|