1+ # Main scene
2+ extends Node
3+
4+ # Create the two peers
5+ var p1 = WebRTCPeerConnection .new ()
6+ var p2 = WebRTCPeerConnection .new ()
7+ var ch1 = p1 .create_data_channel ("chat" , {"id" : 1 , "negotiated" : true })
8+ var ch2 = p2 .create_data_channel ("chat" , {"id" : 1 , "negotiated" : true })
9+
10+ func _ready ():
11+ # Connect P1 session created to itself to set local description
12+ p1 .connect ("session_description_created" , p1 , "set_local_description" )
13+ # Connect P1 session and ICE created to p2 set remote description and candidates
14+ p1 .connect ("session_description_created" , p2 , "set_remote_description" )
15+ p1 .connect ("ice_candidate_created" , p2 , "add_ice_candidate" )
16+
17+ # Same for P2
18+ p2 .connect ("session_description_created" , p2 , "set_local_description" )
19+ p2 .connect ("session_description_created" , p1 , "set_remote_description" )
20+ p2 .connect ("ice_candidate_created" , p1 , "add_ice_candidate" )
21+
22+ # Let P1 create the offer
23+ p1 .create_offer ()
24+
25+ # Wait a second and send message from P1
26+ yield (get_tree ().create_timer (1 ), "timeout" )
27+ ch1 .put_packet ("Hi from P1" .to_utf8 ())
28+
29+ # Wait a second and send message from P2
30+ yield (get_tree ().create_timer (1 ), "timeout" )
31+ ch2 .put_packet ("Hi from P2" .to_utf8 ())
32+
33+ func _process (delta ):
34+ p1 .poll ()
35+ p2 .poll ()
36+ if ch1 .get_ready_state () == ch1 .STATE_OPEN and ch1 .get_available_packet_count () > 0 :
37+ print ("P1 received: " , ch1 .get_packet ().get_string_from_utf8 ())
38+ if ch2 .get_ready_state () == ch2 .STATE_OPEN and ch2 .get_available_packet_count () > 0 :
39+ print ("P2 received: " , ch2 .get_packet ().get_string_from_utf8 ())
0 commit comments