Class: WebRTC::RTCDataChannel

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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, options = {})
  @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 = options[:label] || FFI.webrtc_data_channel_get_label(ptr)
  @ordered = options.fetch(:ordered, true)
  @max_packet_life_time = options[:max_packet_life_time]
  @max_retransmits = options[:max_retransmits]
  @protocol = options[:protocol] || ''
  @negotiated = options.fetch(:negotiated, false)
  @id = options[:id]
end

Instance Attribute Details

#binary_typeObject

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_thresholdObject

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

#idObject (readonly)

Returns the value of attribute id.



8
9
10
# File 'lib/webrtc/data_channel.rb', line 8

def id
  @id
end

#labelObject (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_timeObject (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_retransmitsObject (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

#negotiatedObject (readonly)

Returns the value of attribute negotiated.



8
9
10
# File 'lib/webrtc/data_channel.rb', line 8

def negotiated
  @negotiated
end

#orderedObject (readonly)

Returns the value of attribute ordered.



8
9
10
# File 'lib/webrtc/data_channel.rb', line 8

def ordered
  @ordered
end

#protocolObject (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
    setup_message_callback
  end
  self
end

#buffered_amountObject



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

#closeObject



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

#destroyObject



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 on_message(&block)
  @callbacks[:message] = block
  setup_message_callback
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_stateObject



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

Raises:



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

Raises:



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

Raises:



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_snapshotObject



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