Skip to content

Commit f64d6f5

Browse files
Update WebRTC example code for Godot 4 (godotengine#7882)
Co-authored-by: Hugo Locurcio <[email protected]>
1 parent d99a882 commit f64d6f5

File tree

1 file changed

+42
-34
lines changed

1 file changed

+42
-34
lines changed

tutorials/networking/webrtc.rst

Lines changed: 42 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -64,27 +64,27 @@ This is not very useful in real life, but will give you a good overview of how a
6464
var ch2 = p2.create_data_channel("chat", {"id": 1, "negotiated": true})
6565

6666
func _ready():
67-
# Connect P1 session created to itself to set local description
68-
p1.connect("session_description_created", p1, "set_local_description")
69-
# Connect P1 session and ICE created to p2 set remote description and candidates
70-
p1.connect("session_description_created", p2, "set_remote_description")
71-
p1.connect("ice_candidate_created", p2, "add_ice_candidate")
67+
# Connect P1 session created to itself to set local description.
68+
p1.session_description_created.connect(p1.set_local_description)
69+
# Connect P1 session and ICE created to p2 set remote description and candidates.
70+
p1.session_description_created.connect(p2.set_remote_description)
71+
p1.ice_candidate_created.connect(p2.add_ice_candidate)
7272

7373
# Same for P2
74-
p2.connect("session_description_created", p2, "set_local_description")
75-
p2.connect("session_description_created", p1, "set_remote_description")
76-
p2.connect("ice_candidate_created", p1, "add_ice_candidate")
74+
p2.session_description_created.connect(p2.set_local_description)
75+
p2.session_description_created.connect(p1.set_remote_description)
76+
p2.ice_candidate_created.connect(p1.add_ice_candidate)
7777

7878
# Let P1 create the offer
7979
p1.create_offer()
8080

81-
# Wait a second and send message from P1
82-
yield(get_tree().create_timer(1), "timeout")
83-
ch1.put_packet("Hi from P1".to_utf8())
81+
# Wait a second and send message from P1.
82+
await get_tree().create_timer(1).timeout
83+
ch1.put_packet("Hi from P1".to_utf8_buffer())
8484

85-
# Wait a second and send message from P2
86-
yield(get_tree().create_timer(1), "timeout")
87-
ch2.put_packet("Hi from P2".to_utf8())
85+
# Wait a second and send message from P2.
86+
await get_tree().create_timer(1).timeout
87+
ch2.put_packet("Hi from P2".to_utf8_buffer())
8888

8989
func _process(_delta):
9090
# Poll connections
@@ -111,41 +111,45 @@ This example expands on the previous one, separating the peers in two different
111111

112112
::
113113

114-
# An example P2P chat client (chat.gd)
115114
extends Node
115+
# An example p2p chat client.
116116

117117
var peer = WebRTCPeerConnection.new()
118118

119-
# Create negotiated data channel
119+
# Create negotiated data channel.
120120
var channel = peer.create_data_channel("chat", {"negotiated": true, "id": 1})
121121

122122
func _ready():
123-
# Connect all functions
124-
peer.ice_candidate_created.connect(_on_ice_candidate)
125-
peer.session_description_created.connect(_on_session)
123+
# Connect all functions.
124+
peer.ice_candidate_created.connect(self._on_ice_candidate)
125+
peer.session_description_created.connect(self._on_session)
126+
127+
# Register to the local signaling server (see below for the implementation).
128+
Signaling.register(String(get_path()))
126129

127-
# Register to the local signaling server (see below for the implementation)
128-
Signaling.register(get_path())
129130

130131
func _on_ice_candidate(mid, index, sdp):
131-
# Send the ICE candidate to the other peer via signaling server
132-
Signaling.send_candidate(get_path(), mid, index, sdp)
132+
# Send the ICE candidate to the other peer via signaling server.
133+
Signaling.send_candidate(String(get_path()), mid, index, sdp)
134+
133135

134136
func _on_session(type, sdp):
135-
# Send the session to other peer via signaling server
136-
Signaling.send_session(get_path(), type, sdp)
137-
# Set generated description as local
137+
# Send the session to other peer via signaling server.
138+
Signaling.send_session(String(get_path()), type, sdp)
139+
# Set generated description as local.
138140
peer.set_local_description(type, sdp)
139141

142+
140143
func _process(delta):
141-
# Always poll the connection frequently
144+
# Always poll the connection frequently.
142145
peer.poll()
143146
if channel.get_ready_state() == WebRTCDataChannel.STATE_OPEN:
144147
while channel.get_available_packet_count() > 0:
145-
print(get_path(), " received: ", channel.get_packet().get_string_from_utf8())
148+
print(String(get_path()), " received: ", channel.get_packet().get_string_from_utf8())
149+
146150

147151
func send_message(message):
148-
channel.put_packet(message.to_utf8())
152+
channel.put_packet(message.to_utf8_buffer())
149153

150154
And now for the local signaling server:
151155

@@ -162,22 +166,24 @@ And now for the local signaling server:
162166
func register(path):
163167
assert(peers.size() < 2)
164168
peers.append(path)
165-
# If it's the second one, create an offer
166169
if peers.size() == 2:
167170
get_node(peers[0]).peer.create_offer()
168171

172+
169173
func _find_other(path):
170174
# Find the other registered peer.
171175
for p in peers:
172176
if p != path:
173177
return p
174178
return ""
175179

180+
176181
func send_session(path, type, sdp):
177182
var other = _find_other(path)
178183
assert(other != "")
179184
get_node(other).peer.set_remote_description(type, sdp)
180185

186+
181187
func send_candidate(path, mid, index, sdp):
182188
var other = _find_other(path)
183189
assert(other != "")
@@ -197,12 +203,14 @@ Then you can use it like this:
197203
var p2 = Chat.new()
198204
add_child(p1)
199205
add_child(p2)
200-
yield(get_tree().create_timer(1), "timeout")
201-
p1.send_message("Hi from %s" % p1.get_path())
206+
207+
# Wait a second and send message from P1
208+
await get_tree().create_timer(1).timeout
209+
p1.send_message("Hi from %s" % String(p1.get_path()))
202210

203211
# Wait a second and send message from P2
204-
yield(get_tree().create_timer(1), "timeout")
205-
p2.send_message("Hi from %s" % p2.get_path())
212+
await get_tree().create_timer(1).timeout
213+
p2.send_message("Hi from %s" % String(p2.get_path()))
206214

207215
This will print something similar to this:
208216

0 commit comments

Comments
 (0)