extends Node var peer = WebRTCPeerConnection.new() var wsclient var username # Create negotiated data channel var channel = peer.create_data_channel("position", {"negotiated": true, "id": 1}) func _ready(): # Connect all functions 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) wsclient = find_node("WSClient") wsclient.connect("ws_login_in", self, "on_login_in") wsclient.connect("ws_candidate_in", self, "on_candidate_in") wsclient.connect("ws_session_in", self, "on_session_in") wsclient.login() peer.create_offer() breakpoint func _on_ice_candidate(mid, index, sdp): # Send the ICE candidate to the other peer via signaling server var data = {"mtype": "candidate", "payload": {"mid": mid, "index": index, "sdp": sdp}} emit_signal("ws_out", data) func _on_session(type, sdp): # Send the session to other peer via signaling server var data = {"mtype": "session", "payload": {"type": type, "sdp": sdp}} emit_signal("ws_out", data) # Set generated description as local peer.set_local_description(type, sdp) func _process(delta): # Always poll the connection frequently 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()) func send_message(message): channel.put_packet(message.to_utf8()) func on_session_in(type, sdp): peer.set_remote_description(type, sdp) func on_candidate_in(mid, index, sdp): peer.add_ice_candidate(mid, index, sdp) func on_ws_login_in(msg): print(msg)