minimal.gd 1.5 KB

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