godot-demo-projects/networking/webrtc_minimal/chat.gd

40 lines
1.2 KiB
GDScript3
Raw Normal View History

2019-06-14 04:01:57 +02:00
extends Node
# An example p2p chat client.
2019-06-14 04:01:57 +02:00
var peer = WebRTCPeerConnection.new()
# Create negotiated data channel.
2019-06-14 04:01:57 +02:00
var channel = peer.create_data_channel("chat", {"negotiated": true, "id": 1})
func _ready():
# Connect all functions.
2019-06-14 04:01:57 +02:00
peer.connect("ice_candidate_created", self, "_on_ice_candidate")
peer.connect("session_description_created", self, "_on_session")
# Register to the local signaling server (see below for the implementation).
2019-06-14 04:01:57 +02:00
Signaling.register(get_path())
2019-06-14 04:01:57 +02:00
func _on_ice_candidate(mid, index, sdp):
# Send the ICE candidate to the other peer via signaling server.
2019-06-14 04:01:57 +02:00
Signaling.send_candidate(get_path(), mid, index, sdp)
2019-06-14 04:01:57 +02:00
func _on_session(type, sdp):
# Send the session to other peer via signaling server.
2019-06-14 04:01:57 +02:00
Signaling.send_session(get_path(), type, sdp)
# Set generated description as local.
2019-06-14 04:01:57 +02:00
peer.set_local_description(type, sdp)
2019-06-14 04:01:57 +02:00
func _process(delta):
# Always poll the connection frequently.
2019-06-14 04:01:57 +02:00
peer.poll()
if channel.get_ready_state() == WebRTCDataChannel.STATE_OPEN:
while channel.get_available_packet_count() > 0:
print(get_path(), " received: ", channel.get_packet().get_string_from_utf8())
2019-06-14 04:01:57 +02:00
func send_message(message):
channel.put_packet(message.to_utf8())