Class: Factor::WebSocketManager
- Inherits:
-
Object
- Object
- Factor::WebSocketManager
- Defined in:
- lib/websocket_manager.rb
Overview
class for managing the web socket connections
Instance Attribute Summary collapse
-
#events ⇒ Object
Returns the value of attribute events.
-
#keep_open ⇒ Object
Returns the value of attribute keep_open.
-
#state ⇒ Object
Returns the value of attribute state.
Instance Method Summary collapse
- #close ⇒ Object
- #closed? ⇒ Boolean
- #closing? ⇒ Boolean
-
#initialize(uri, headers = {}) ⇒ WebSocketManager
constructor
A new instance of WebSocketManager.
- #on(event, &block) ⇒ Object
- #open ⇒ Object
- #open? ⇒ Boolean
- #opening? ⇒ Boolean
- #send(msg) ⇒ Object
Constructor Details
#initialize(uri, headers = {}) ⇒ WebSocketManager
Returns a new instance of WebSocketManager.
12 13 14 15 16 17 18 19 |
# File 'lib/websocket_manager.rb', line 12 def initialize(uri, headers = {}) u = URI(uri) @uri = u.to_s @settings = { ping: 10, retry: 5 } @settings[:headers] = headers if headers && headers != {} @state = :closed @events = {} end |
Instance Attribute Details
#events ⇒ Object
Returns the value of attribute events.
10 11 12 |
# File 'lib/websocket_manager.rb', line 10 def events @events end |
#keep_open ⇒ Object
Returns the value of attribute keep_open.
10 11 12 |
# File 'lib/websocket_manager.rb', line 10 def keep_open @keep_open end |
#state ⇒ Object
Returns the value of attribute state.
10 11 12 |
# File 'lib/websocket_manager.rb', line 10 def state @state end |
Instance Method Details
#close ⇒ Object
29 30 31 32 33 34 35 |
# File 'lib/websocket_manager.rb', line 29 def close if open? @state = :closing @ws.close end @state end |
#closed? ⇒ Boolean
49 50 51 |
# File 'lib/websocket_manager.rb', line 49 def closed? @state == :closed end |
#closing? ⇒ Boolean
53 54 55 |
# File 'lib/websocket_manager.rb', line 53 def closing? @state == :closing end |
#on(event, &block) ⇒ Object
37 38 39 |
# File 'lib/websocket_manager.rb', line 37 def on(event, &block) @events[event] = block end |
#open ⇒ Object
21 22 23 24 25 26 27 |
# File 'lib/websocket_manager.rb', line 21 def open if closed? @state = :opening connect end @state end |
#open? ⇒ Boolean
41 42 43 |
# File 'lib/websocket_manager.rb', line 41 def open? @state == :open end |
#opening? ⇒ Boolean
45 46 47 |
# File 'lib/websocket_manager.rb', line 45 def opening? @state == :opening end |
#send(msg) ⇒ Object
57 58 59 |
# File 'lib/websocket_manager.rb', line 57 def send(msg) @ws.send(msg.to_json) end |