Class: Pinnacle::Wrapper::Voice::Client::DefaultSocket
- Inherits:
-
Object
- Object
- Pinnacle::Wrapper::Voice::Client::DefaultSocket
- Defined in:
- lib/pinnacle/wrapper/voice/client.rb
Instance Attribute Summary collapse
-
#ready_state ⇒ Object
readonly
Returns the value of attribute ready_state.
Instance Method Summary collapse
- #add_event_listener(event, &listener) ⇒ Object
- #close ⇒ Object
-
#initialize(url, protocols = nil) ⇒ DefaultSocket
constructor
A new instance of DefaultSocket.
- #send(data) ⇒ Object
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) { || emit("message", { data: .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_state ⇒ Object (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 |
#close ⇒ Object
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 |