minimal.gd 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  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.session_description_created.connect(p1.set_local_description)
  12. # Connect P1 session and ICE created to p2 set remote description and candidates.
  13. p1.session_description_created.connect(p2.set_remote_description)
  14. p1.ice_candidate_created.connect(p2.add_ice_candidate)
  15. # Same for P2.
  16. p2.session_description_created.connect(p2.set_local_description)
  17. p2.session_description_created.connect(p1.set_remote_description)
  18. p2.ice_candidate_created.connect(p1.add_ice_candidate)
  19. # Let P1 create the offer.
  20. p1.create_offer()
  21. # Wait a second and send message from P1.
  22. await get_tree().create_timer(1).timeout
  23. ch1.put_packet("Hi from P1".to_utf8_buffer())
  24. # Wait a second and send message from P2.
  25. await get_tree().create_timer(1).timeout
  26. ch2.put_packet("Hi from P2".to_utf8_buffer())
  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())