|
@@ -64,27 +64,27 @@ This is not very useful in real life, but will give you a good overview of how a
|
|
|
var ch2 = p2.create_data_channel("chat", {"id": 1, "negotiated": true})
|
|
|
|
|
|
func _ready():
|
|
|
- # Connect P1 session created to itself to set local description
|
|
|
- p1.connect("session_description_created", p1, "set_local_description")
|
|
|
- # Connect P1 session and ICE created to p2 set remote description and candidates
|
|
|
- p1.connect("session_description_created", p2, "set_remote_description")
|
|
|
- p1.connect("ice_candidate_created", p2, "add_ice_candidate")
|
|
|
+ # Connect P1 session created to itself to set local description.
|
|
|
+ p1.session_description_created.connect(p1.set_local_description)
|
|
|
+ # Connect P1 session and ICE created to p2 set remote description and candidates.
|
|
|
+ p1.session_description_created.connect(p2.set_remote_description)
|
|
|
+ p1.ice_candidate_created.connect(p2.add_ice_candidate)
|
|
|
|
|
|
# Same for P2
|
|
|
- p2.connect("session_description_created", p2, "set_local_description")
|
|
|
- p2.connect("session_description_created", p1, "set_remote_description")
|
|
|
- p2.connect("ice_candidate_created", p1, "add_ice_candidate")
|
|
|
+ p2.session_description_created.connect(p2.set_local_description)
|
|
|
+ p2.session_description_created.connect(p1.set_remote_description)
|
|
|
+ p2.ice_candidate_created.connect(p1.add_ice_candidate)
|
|
|
|
|
|
# Let P1 create the offer
|
|
|
p1.create_offer()
|
|
|
|
|
|
- # Wait a second and send message from P1
|
|
|
- yield(get_tree().create_timer(1), "timeout")
|
|
|
- ch1.put_packet("Hi from P1".to_utf8())
|
|
|
+ # Wait a second and send message from P1.
|
|
|
+ await get_tree().create_timer(1).timeout
|
|
|
+ ch1.put_packet("Hi from P1".to_utf8_buffer())
|
|
|
|
|
|
- # Wait a second and send message from P2
|
|
|
- yield(get_tree().create_timer(1), "timeout")
|
|
|
- ch2.put_packet("Hi from P2".to_utf8())
|
|
|
+ # Wait a second and send message from P2.
|
|
|
+ await get_tree().create_timer(1).timeout
|
|
|
+ ch2.put_packet("Hi from P2".to_utf8_buffer())
|
|
|
|
|
|
func _process(_delta):
|
|
|
# Poll connections
|
|
@@ -111,41 +111,45 @@ This example expands on the previous one, separating the peers in two different
|
|
|
|
|
|
::
|
|
|
|
|
|
- # An example P2P chat client (chat.gd)
|
|
|
extends Node
|
|
|
+ # An example p2p chat client.
|
|
|
|
|
|
var peer = WebRTCPeerConnection.new()
|
|
|
|
|
|
- # Create negotiated data channel
|
|
|
+ # Create negotiated data channel.
|
|
|
var channel = peer.create_data_channel("chat", {"negotiated": true, "id": 1})
|
|
|
|
|
|
func _ready():
|
|
|
- # Connect all functions
|
|
|
- peer.ice_candidate_created.connect(_on_ice_candidate)
|
|
|
- peer.session_description_created.connect(_on_session)
|
|
|
+ # Connect all functions.
|
|
|
+ peer.ice_candidate_created.connect(self._on_ice_candidate)
|
|
|
+ peer.session_description_created.connect(self._on_session)
|
|
|
+
|
|
|
+ # Register to the local signaling server (see below for the implementation).
|
|
|
+ Signaling.register(String(get_path()))
|
|
|
|
|
|
- # Register to the local signaling server (see below for the implementation)
|
|
|
- Signaling.register(get_path())
|
|
|
|
|
|
func _on_ice_candidate(mid, index, sdp):
|
|
|
- # Send the ICE candidate to the other peer via signaling server
|
|
|
- Signaling.send_candidate(get_path(), mid, index, sdp)
|
|
|
+ # Send the ICE candidate to the other peer via signaling server.
|
|
|
+ Signaling.send_candidate(String(get_path()), mid, index, sdp)
|
|
|
+
|
|
|
|
|
|
func _on_session(type, sdp):
|
|
|
- # Send the session to other peer via signaling server
|
|
|
- Signaling.send_session(get_path(), type, sdp)
|
|
|
- # Set generated description as local
|
|
|
+ # Send the session to other peer via signaling server.
|
|
|
+ Signaling.send_session(String(get_path()), type, sdp)
|
|
|
+ # Set generated description as local.
|
|
|
peer.set_local_description(type, sdp)
|
|
|
|
|
|
+
|
|
|
func _process(delta):
|
|
|
- # Always poll the connection frequently
|
|
|
+ # Always poll the connection frequently.
|
|
|
peer.poll()
|
|
|
if channel.get_ready_state() == WebRTCDataChannel.STATE_OPEN:
|
|
|
while channel.get_available_packet_count() > 0:
|
|
|
- print(get_path(), " received: ", channel.get_packet().get_string_from_utf8())
|
|
|
+ print(String(get_path()), " received: ", channel.get_packet().get_string_from_utf8())
|
|
|
+
|
|
|
|
|
|
func send_message(message):
|
|
|
- channel.put_packet(message.to_utf8())
|
|
|
+ channel.put_packet(message.to_utf8_buffer())
|
|
|
|
|
|
And now for the local signaling server:
|
|
|
|
|
@@ -162,10 +166,10 @@ And now for the local signaling server:
|
|
|
func register(path):
|
|
|
assert(peers.size() < 2)
|
|
|
peers.append(path)
|
|
|
- # If it's the second one, create an offer
|
|
|
if peers.size() == 2:
|
|
|
get_node(peers[0]).peer.create_offer()
|
|
|
|
|
|
+
|
|
|
func _find_other(path):
|
|
|
# Find the other registered peer.
|
|
|
for p in peers:
|
|
@@ -173,11 +177,13 @@ And now for the local signaling server:
|
|
|
return p
|
|
|
return ""
|
|
|
|
|
|
+
|
|
|
func send_session(path, type, sdp):
|
|
|
var other = _find_other(path)
|
|
|
assert(other != "")
|
|
|
get_node(other).peer.set_remote_description(type, sdp)
|
|
|
|
|
|
+
|
|
|
func send_candidate(path, mid, index, sdp):
|
|
|
var other = _find_other(path)
|
|
|
assert(other != "")
|
|
@@ -197,12 +203,14 @@ Then you can use it like this:
|
|
|
var p2 = Chat.new()
|
|
|
add_child(p1)
|
|
|
add_child(p2)
|
|
|
- yield(get_tree().create_timer(1), "timeout")
|
|
|
- p1.send_message("Hi from %s" % p1.get_path())
|
|
|
+
|
|
|
+ # Wait a second and send message from P1
|
|
|
+ await get_tree().create_timer(1).timeout
|
|
|
+ p1.send_message("Hi from %s" % String(p1.get_path()))
|
|
|
|
|
|
# Wait a second and send message from P2
|
|
|
- yield(get_tree().create_timer(1), "timeout")
|
|
|
- p2.send_message("Hi from %s" % p2.get_path())
|
|
|
+ await get_tree().create_timer(1).timeout
|
|
|
+ p2.send_message("Hi from %s" % String(p2.get_path()))
|
|
|
|
|
|
This will print something similar to this:
|
|
|
|