Class: Pinnacle::Wrapper::Voice::Client::DefaultSocket

Inherits:
Object
  • Object
show all
Defined in:
lib/pinnacle/wrapper/voice/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, protocols = nil) ⇒ DefaultSocket

Returns a new instance of DefaultSocket.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/pinnacle/wrapper/voice/client.rb', line 15

def initialize(url, protocols = nil)
  @ready_state = 0
  @listeners = Hash.new { |hash, key| hash[key] = [] }
  headers = if protocols.nil?
              {}
            else
              { "Sec-WebSocket-Protocol" => Array(protocols).join(", ") }
            end
  @socket = WebSocket::Client::Simple.connect(url, headers: headers)
  @socket.on(:open) do |event|
    @ready_state = 1
    emit("open", event)
  end
  @socket.on(:message) { |message| emit("message", { data: message.data }) }
  @socket.on(:error) { |error| emit("error", error) }
  @socket.on(:close) do |event|
    @ready_state = 3
    emit("close", event)
  end
end

Instance Attribute Details

#ready_stateObject (readonly)

Returns the value of attribute ready_state.



13
14
15
# File 'lib/pinnacle/wrapper/voice/client.rb', line 13

def ready_state
  @ready_state
end

Instance Method Details

#add_event_listener(event, &listener) ⇒ Object



36
37
38
# File 'lib/pinnacle/wrapper/voice/client.rb', line 36

def add_event_listener(event, &listener)
  @listeners[event.to_s] << listener
end

#closeObject



44
45
46
# File 'lib/pinnacle/wrapper/voice/client.rb', line 44

def close(*)
  @socket.close
end

#send(data) ⇒ Object



40
41
42
# File 'lib/pinnacle/wrapper/voice/client.rb', line 40

def send(data)
  @socket.send(data)
end