chat.gd 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. extends Node
  2. # An example p2p chat client.
  3. var peer = WebRTCPeerConnection.new()
  4. # Create negotiated data channel.
  5. var channel = peer.create_data_channel("chat", {"negotiated": true, "id": 1})
  6. func _ready():
  7. # Connect all functions.
  8. peer.ice_candidate_created.connect(self._on_ice_candidate)
  9. peer.session_description_created.connect(self._on_session)
  10. # Register to the local signaling server (see below for the implementation).
  11. Signaling.register(String(get_path()))
  12. func _on_ice_candidate(mid, index, sdp):
  13. # Send the ICE candidate to the other peer via signaling server.
  14. Signaling.send_candidate(String(get_path()), mid, index, sdp)
  15. func _on_session(type, sdp):
  16. # Send the session to other peer via signaling server.
  17. Signaling.send_session(String(get_path()), type, sdp)
  18. # Set generated description as local.
  19. peer.set_local_description(type, sdp)
  20. func _process(delta):
  21. # Always poll the connection frequently.
  22. peer.poll()
  23. if channel.get_ready_state() == WebRTCDataChannel.STATE_OPEN:
  24. while channel.get_available_packet_count() > 0:
  25. print(String(get_path()), " received: ", channel.get_packet().get_string_from_utf8())
  26. func send_message(message):
  27. channel.put_packet(message.to_utf8_buffer())