minimal.gd 1.6 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. print(p1.create_data_channel("chat", {"id": 1, "negotiated": true}))
  10. # Connect P1 session created to itself to set local description.
  11. p1.connect("session_description_created", p1, "set_local_description")
  12. # Connect P1 session and ICE created to p2 set remote description and candidates.
  13. p1.connect("session_description_created", p2, "set_remote_description")
  14. p1.connect("ice_candidate_created", p2, "add_ice_candidate")
  15. # Same for P2.
  16. p2.connect("session_description_created", p2, "set_local_description")
  17. p2.connect("session_description_created", p1, "set_remote_description")
  18. p2.connect("ice_candidate_created", p1, "add_ice_candidate")
  19. # Let P1 create the offer.
  20. p1.create_offer()
  21. # Wait a second and send message from P1.
  22. yield(get_tree().create_timer(1), "timeout")
  23. ch1.put_packet("Hi from P1".to_utf8())
  24. # Wait a second and send message from P2.
  25. yield(get_tree().create_timer(1), "timeout")
  26. ch2.put_packet("Hi from P2".to_utf8())
  27. func _process(delta):
  28. p1.poll()
  29. p2.poll()
  30. if ch1.get_ready_state() == ch1.STATE_OPEN and ch1.get_available_packet_count() > 0:
  31. print("P1 received: ", ch1.get_packet().get_string_from_utf8())
  32. if ch2.get_ready_state() == ch2.STATE_OPEN and ch2.get_available_packet_count() > 0:
  33. print("P2 received: ", ch2.get_packet().get_string_from_utf8())