From ddafc749254cf17727682e7f99ddf924e649e9fb Mon Sep 17 00:00:00 2001 From: Peter Lindgren Date: Tue, 5 Sep 2023 21:35:55 +0200 Subject: [PATCH 1/2] Update webrtc.rst Updated gdscript examples to match the godot-demo-projects repository code: https://github.com/godotengine/godot-demo-projects/tree/4.0/networking/webrtc_minimal --- tutorials/networking/webrtc.rst | 154 +++++++++++++++++--------------- 1 file changed, 81 insertions(+), 73 deletions(-) diff --git a/tutorials/networking/webrtc.rst b/tutorials/networking/webrtc.rst index 18829a67a31..30c7f273ee5 100644 --- a/tutorials/networking/webrtc.rst +++ b/tutorials/networking/webrtc.rst @@ -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 P2 - yield(get_tree().create_timer(1), "timeout") - ch2.put_packet("Hi from P2".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. + 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) - - # Register to the local signaling server (see below for the implementation) - Signaling.register(get_path()) - + # 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())) + + 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 - peer.set_local_description(type, sdp) - + # 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 - peer.poll() - if channel.get_ready_state() == WebRTCDataChannel.STATE_OPEN: + # 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: @@ -155,33 +159,35 @@ And now for the local signaling server: # A local signaling server. Add this to autoloads with name "Signaling" (/root/Signaling) extends Node - + # We will store the two peers here var peers = [] - + func register(path): - assert(peers.size() < 2) - peers.append(path) - # If it's the second one, create an offer - if peers.size() == 2: + assert(peers.size() < 2) + peers.append(path) + if peers.size() == 2: get_node(peers[0]).peer.create_offer() - + + func _find_other(path): - # Find the other registered peer. + # Find the other registered peer. for p in peers: if p != path: return p - return "" - + return "" + + func send_session(path, type, sdp): - var other = _find_other(path) - assert(other != "") - get_node(other).peer.set_remote_description(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 != "") - get_node(other).peer.add_ice_candidate(mid, index, sdp) + var other = _find_other(path) + assert(other != "") + get_node(other).peer.add_ice_candidate(mid, index, sdp) Then you can use it like this: @@ -189,20 +195,22 @@ Then you can use it like this: # Main scene (main.gd) extends Node - + const Chat = preload("res://chat.gd") - + func _ready(): - var p1 = Chat.new() - 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 P2 - yield(get_tree().create_timer(1), "timeout") - p2.send_message("Hi from %s" % p2.get_path()) + var p1 = Chat.new() + var p2 = Chat.new() + add_child(p1) + add_child(p2) + + # 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 + await get_tree().create_timer(1).timeout + p2.send_message("Hi from %s" % String(p2.get_path())) This will print something similar to this: From 2caa720237067a650c552a241de73c523069feeb Mon Sep 17 00:00:00 2001 From: Hugo Locurcio Date: Thu, 9 Nov 2023 12:43:05 +0100 Subject: [PATCH 2/2] Convert indentation to spaces, remove trailing whitespace --- tutorials/networking/webrtc.rst | 128 ++++++++++++++++---------------- 1 file changed, 64 insertions(+), 64 deletions(-) diff --git a/tutorials/networking/webrtc.rst b/tutorials/networking/webrtc.rst index 30c7f273ee5..903405d3c40 100644 --- a/tutorials/networking/webrtc.rst +++ b/tutorials/networking/webrtc.rst @@ -69,19 +69,19 @@ This is not very useful in real life, but will give you a good overview of how a # 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.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. await get_tree().create_timer(1).timeout ch1.put_packet("Hi from P1".to_utf8_buffer()) - + # Wait a second and send message from P2. await get_tree().create_timer(1).timeout ch2.put_packet("Hi from P2".to_utf8_buffer()) @@ -113,43 +113,43 @@ This example expands on the previous one, separating the peers in two different extends Node # An example p2p chat client. - + var peer = WebRTCPeerConnection.new() - + # 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(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())) - - + # 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())) + + func _on_ice_candidate(mid, index, sdp): - # Send the ICE candidate to the other peer via signaling server. - Signaling.send_candidate(String(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(String(get_path()), type, sdp) - # Set generated description as local. - peer.set_local_description(type, sdp) - - + # 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. - peer.poll() - if channel.get_ready_state() == WebRTCDataChannel.STATE_OPEN: + # Always poll the connection frequently. + peer.poll() + if channel.get_ready_state() == WebRTCDataChannel.STATE_OPEN: while channel.get_available_packet_count() > 0: print(String(get_path()), " received: ", channel.get_packet().get_string_from_utf8()) - - + + func send_message(message): - channel.put_packet(message.to_utf8_buffer()) + channel.put_packet(message.to_utf8_buffer()) And now for the local signaling server: @@ -159,35 +159,35 @@ And now for the local signaling server: # A local signaling server. Add this to autoloads with name "Signaling" (/root/Signaling) extends Node - + # We will store the two peers here var peers = [] - + func register(path): - assert(peers.size() < 2) - peers.append(path) - if peers.size() == 2: + assert(peers.size() < 2) + peers.append(path) + if peers.size() == 2: get_node(peers[0]).peer.create_offer() - - + + func _find_other(path): - # Find the other registered peer. + # Find the other registered peer. for p in peers: if p != path: return p - return "" - - + return "" + + func send_session(path, type, sdp): - var other = _find_other(path) - assert(other != "") - get_node(other).peer.set_remote_description(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 != "") - get_node(other).peer.add_ice_candidate(mid, index, sdp) + var other = _find_other(path) + assert(other != "") + get_node(other).peer.add_ice_candidate(mid, index, sdp) Then you can use it like this: @@ -195,22 +195,22 @@ Then you can use it like this: # Main scene (main.gd) extends Node - + const Chat = preload("res://chat.gd") - + func _ready(): - var p1 = Chat.new() - var p2 = Chat.new() - add_child(p1) - add_child(p2) - - # 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 - await get_tree().create_timer(1).timeout - p2.send_message("Hi from %s" % String(p2.get_path())) + var p1 = Chat.new() + var p2 = Chat.new() + add_child(p1) + add_child(p2) + + # 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 + await get_tree().create_timer(1).timeout + p2.send_message("Hi from %s" % String(p2.get_path())) This will print something similar to this: