Class: WebRTC::RTCDataChannel
- Inherits:
-
Object
- Object
- WebRTC::RTCDataChannel
- Defined in:
- lib/webrtc/data_channel.rb
Constant Summary collapse
- READY_STATES =
%i[connecting open closing closed].freeze
- BINARY_TYPES =
%i[blob arraybuffer].freeze
Instance Attribute Summary collapse
-
#binary_type ⇒ Object
Returns the value of attribute binary_type.
-
#buffered_amount_low_threshold ⇒ Object
Returns the value of attribute buffered_amount_low_threshold.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#label ⇒ Object
readonly
Returns the value of attribute label.
-
#max_packet_life_time ⇒ Object
readonly
Returns the value of attribute max_packet_life_time.
-
#max_retransmits ⇒ Object
readonly
Returns the value of attribute max_retransmits.
-
#negotiated ⇒ Object
readonly
Returns the value of attribute negotiated.
-
#ordered ⇒ Object
readonly
Returns the value of attribute ordered.
-
#protocol ⇒ Object
readonly
Returns the value of attribute protocol.
Instance Method Summary collapse
- #add_internal_listener(event, &block) ⇒ Object
- #buffered_amount ⇒ Object
- #close ⇒ Object
- #destroy ⇒ Object
-
#initialize(ptr, options = {}) ⇒ RTCDataChannel
constructor
A new instance of RTCDataChannel.
- #observe(observer) ⇒ Object
- #on_buffered_amount_low(&block) ⇒ Object
- #on_close(&block) ⇒ Object
- #on_error(&block) ⇒ Object
- #on_message(&block) ⇒ Object
- #on_open(&block) ⇒ Object
- #ready_state ⇒ Object
- #send(data) ⇒ Object
- #send_binary(data) ⇒ Object
- #send_text(data) ⇒ Object
- #stats_snapshot ⇒ Object
Constructor Details
#initialize(ptr, options = {}) ⇒ RTCDataChannel
Returns a new instance of RTCDataChannel.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/webrtc/data_channel.rb', line 11 def initialize(ptr, = {}) @ptr = ptr @callbacks = {} @internal_callbacks = Hash.new { |h, k| h[k] = [] } @native_callbacks_setup = {} @binary_type = :arraybuffer @buffered_amount_low_threshold = 0 @messages_sent = 0 @bytes_sent = 0 @messages_received = 0 @bytes_received = 0 @label = [:label] || FFI.webrtc_data_channel_get_label(ptr) @ordered = .fetch(:ordered, true) @max_packet_life_time = [:max_packet_life_time] @max_retransmits = [:max_retransmits] @protocol = [:protocol] || '' @negotiated = .fetch(:negotiated, false) @id = [:id] end |
Instance Attribute Details
#binary_type ⇒ Object
Returns the value of attribute binary_type.
9 10 11 |
# File 'lib/webrtc/data_channel.rb', line 9 def binary_type @binary_type end |
#buffered_amount_low_threshold ⇒ Object
Returns the value of attribute buffered_amount_low_threshold.
9 10 11 |
# File 'lib/webrtc/data_channel.rb', line 9 def buffered_amount_low_threshold @buffered_amount_low_threshold end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
8 9 10 |
# File 'lib/webrtc/data_channel.rb', line 8 def id @id end |
#label ⇒ Object (readonly)
Returns the value of attribute label.
8 9 10 |
# File 'lib/webrtc/data_channel.rb', line 8 def label @label end |
#max_packet_life_time ⇒ Object (readonly)
Returns the value of attribute max_packet_life_time.
8 9 10 |
# File 'lib/webrtc/data_channel.rb', line 8 def max_packet_life_time @max_packet_life_time end |
#max_retransmits ⇒ Object (readonly)
Returns the value of attribute max_retransmits.
8 9 10 |
# File 'lib/webrtc/data_channel.rb', line 8 def max_retransmits @max_retransmits end |
#negotiated ⇒ Object (readonly)
Returns the value of attribute negotiated.
8 9 10 |
# File 'lib/webrtc/data_channel.rb', line 8 def negotiated @negotiated end |
#ordered ⇒ Object (readonly)
Returns the value of attribute ordered.
8 9 10 |
# File 'lib/webrtc/data_channel.rb', line 8 def ordered @ordered end |
#protocol ⇒ Object (readonly)
Returns the value of attribute protocol.
8 9 10 |
# File 'lib/webrtc/data_channel.rb', line 8 def protocol @protocol end |
Instance Method Details
#add_internal_listener(event, &block) ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/webrtc/data_channel.rb', line 106 def add_internal_listener(event, &block) return self unless block @internal_callbacks[event] << block case event when :open setup_open_callback when :close setup_close_callback when :message end self end |
#buffered_amount ⇒ Object
39 40 41 42 43 |
# File 'lib/webrtc/data_channel.rb', line 39 def buffered_amount return 0 if @ptr.nil? FFI.webrtc_data_channel_get_buffered_amount(@ptr) end |
#close ⇒ Object
64 65 66 67 68 |
# File 'lib/webrtc/data_channel.rb', line 64 def close return if @ptr.nil? FFI.webrtc_data_channel_close(@ptr) end |
#destroy ⇒ Object
70 71 72 73 74 75 |
# File 'lib/webrtc/data_channel.rb', line 70 def destroy return if @ptr.nil? FFI.webrtc_data_channel_destroy(@ptr) @ptr = nil end |
#observe(observer) ⇒ Object
100 101 102 103 104 |
# File 'lib/webrtc/data_channel.rb', line 100 def observe(observer) return self unless observer.respond_to?(:bind) observer.bind(self) end |
#on_buffered_amount_low(&block) ⇒ Object
96 97 98 |
# File 'lib/webrtc/data_channel.rb', line 96 def on_buffered_amount_low(&block) @callbacks[:buffered_amount_low] = block end |
#on_close(&block) ⇒ Object
82 83 84 85 |
# File 'lib/webrtc/data_channel.rb', line 82 def on_close(&block) @callbacks[:close] = block setup_close_callback end |
#on_error(&block) ⇒ Object
87 88 89 |
# File 'lib/webrtc/data_channel.rb', line 87 def on_error(&block) @callbacks[:error] = block end |
#on_message(&block) ⇒ Object
91 92 93 94 |
# File 'lib/webrtc/data_channel.rb', line 91 def (&block) @callbacks[:message] = block end |
#on_open(&block) ⇒ Object
77 78 79 80 |
# File 'lib/webrtc/data_channel.rb', line 77 def on_open(&block) @callbacks[:open] = block setup_open_callback end |
#ready_state ⇒ Object
32 33 34 35 36 37 |
# File 'lib/webrtc/data_channel.rb', line 32 def ready_state return :closed if @ptr.nil? state_index = FFI.webrtc_data_channel_get_ready_state(@ptr) READY_STATES[state_index] || :unknown end |
#send(data) ⇒ Object
45 46 47 48 49 50 |
# File 'lib/webrtc/data_channel.rb', line 45 def send(data) raise InvalidStateError, 'DataChannel is not open' unless ready_state == :open is_binary = data.is_a?(String) && data.encoding == Encoding::BINARY send_data(data, is_binary) end |
#send_binary(data) ⇒ Object
58 59 60 61 62 |
# File 'lib/webrtc/data_channel.rb', line 58 def send_binary(data) raise InvalidStateError, 'DataChannel is not open' unless ready_state == :open send_data(data, true) end |
#send_text(data) ⇒ Object
52 53 54 55 56 |
# File 'lib/webrtc/data_channel.rb', line 52 def send_text(data) raise InvalidStateError, 'DataChannel is not open' unless ready_state == :open send_data(data.to_s, false) end |
#stats_snapshot ⇒ Object
121 122 123 124 125 126 127 128 129 |
# File 'lib/webrtc/data_channel.rb', line 121 def stats_snapshot { messages_sent: @messages_sent, bytes_sent: @bytes_sent, messages_received: @messages_received, bytes_received: @bytes_received, state: ready_state } end |