godot-webrtc-mplayer-testing/multiplayer_client.gd

109 lines
3.6 KiB
GDScript3
Raw Normal View History

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-12-03 17:05:31 -06:00
var multiplayer_url = "ws://webrtc-signaller.deno.dev"
# var multiplayer_url = "ws://localhost:8888"
# var multiplayer_url = "ws://echo.websocket.org"
2021-11-17 08:18:12 -06:00
var webrtc_ice_servers = [
2021-12-02 15:13:12 -06:00
{ "urls": ["stun:stun.l.google.com:19302"] },
# { "urls": ["stun:localhost:3478"] }
2021-11-17 08:18:12 -06:00
]
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():
2021-11-17 16:24:00 -06:00
# signaller.connect("websocket_connected", self, "connected")
# signaller.connect("websocket_disconnected", self, "disconnected")
2021-11-15 15:02:55 -06:00
2021-11-17 16:24:00 -06:00
signaller.connect("offer_received", self, "offer_received")
signaller.connect("answer_received", self, "answer_received")
signaller.connect("candidate_received", self, "candidate_received")
2021-11-17 08:18:12 -06:00
2021-12-02 16:09:43 -06:00
signaller.connect("peer_id_set", self, "_peer_id_set")
2021-11-17 08:18:12 -06:00
# connect("lobby_sealed", self, "lobby_sealed")
2021-11-17 16:24:00 -06:00
signaller.connect("peer_joined", self, "_peer_joined")
2021-11-17 08:18:12 -06:00
# 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
2021-11-17 16:24:00 -06:00
func _create_peer(peer_id):
2021-12-02 16:09:43 -06:00
# if peer_id == signaller.peer_id: return
2021-11-15 15:02:55 -06:00
var peer: WebRTCPeerConnection = WebRTCPeerConnection.new()
2021-12-02 16:09:43 -06:00
print("New Net Peer with peer_id %d" % [peer_id])
2021-11-17 16:24:00 -06:00
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()
2021-12-02 15:13:12 -06:00
print("Create Offer Result: %s" % offer_result)
2021-11-17 16:24:00 -06:00
else:
print("Did not create offer: %d <= %d" % [peer_id, uid])
2021-11-15 15:02:55 -06:00
return peer
2021-11-17 16:24:00 -06:00
func _new_ice_candidate(mid_name, index_name, sdp_name, peer_id):
2021-12-02 15:13:12 -06:00
print("New ICE Candidate - MID: %s, Index: %s, SDP: %s, PeerID: %s" % [mid_name, index_name, sdp_name, peer_id])
2021-11-17 16:24:00 -06:00
signaller.send_candidate(peer_id, mid_name, index_name, sdp_name)
2021-11-15 15:02:55 -06:00
2021-11-17 16:24:00 -06:00
func _offer_created(type, data, peer_id):
2021-12-02 15:13:12 -06:00
print("Offer created - Type: %s, PeerID: %s, Data: %s" % [type, peer_id, JSON.print(data)])
2021-11-17 16:24:00 -06:00
if not mp.has_peer(peer_id):
2021-11-15 15:02:55 -06:00
return
2021-11-17 16:24:00 -06:00
mp.get_peer(peer_id).connection.set_local_description(type, data)
2021-11-17 08:18:12 -06:00
if type == "offer":
2021-12-02 15:13:12 -06:00
signaller.send_offer(peer_id, data)
2021-11-17 08:18:12 -06:00
else:
2021-12-02 15:13:12 -06:00
signaller.send_answer(peer_id, data)
2021-11-15 15:02:55 -06:00
2021-12-02 16:09:43 -06:00
func _peer_id_set(peer_id):
print("Peer ID received %s - setting up to peer..." % [peer_id])
2021-11-17 16:24:00 -06:00
mp.initialize(peer_id, true)
2021-12-02 15:13:12 -06:00
get_tree().network_peer = mp
2021-11-15 15:02:55 -06:00
2021-11-17 16:24:00 -06:00
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)
2021-11-15 15:02:55 -06:00
func peer_disconnected(id):
2021-12-02 15:13:12 -06:00
print("Peer %s 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
2021-11-17 16:24:00 -06:00
func offer_received(data):
var id = data["peerId"]
2021-11-17 08:18:12 -06:00
if mp.has_peer(id):
2021-12-02 15:13:12 -06:00
print("Setting offer remote description: %s" % JSON.print(data))
2021-11-17 16:24:00 -06:00
mp.get_peer(id).connection.set_remote_description("offer", data["offer"])
2021-12-02 16:09:43 -06:00
else:
print("Received an offer for a peer with ID %s that hasn't been added" % id)
2021-11-15 15:02:55 -06:00
2021-11-17 16:24:00 -06:00
func answer_received(data):
var id = data["peerId"]
2021-11-17 08:18:12 -06:00
if mp.has_peer(id):
2021-12-02 15:13:12 -06:00
print("Setting answer remote description: %s" % JSON.print(data))
mp.get_peer(id).connection.set_remote_description("answer", data["answer"])
2021-11-15 15:02:55 -06:00
2021-11-17 16:24:00 -06:00
func candidate_received(data):
var id = data["peerId"]
2021-11-17 08:18:12 -06:00
if mp.has_peer(id):
2021-12-02 15:13:12 -06:00
print("Adding ice candidate: %s" % JSON.print(data))
2021-11-17 16:24:00 -06:00
mp.get_peer(id).connection.add_ice_candidate(data["mid"], data["index"], data["sdp"])
2021-12-02 15:13:12 -06:00
else:
print("Received candidate for non-existant peer %s" % id)