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.
11 12 13 14 15 16 17 18 |
# File 'lib/websocket_manager.rb', line 11 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.
9 10 11 |
# File 'lib/websocket_manager.rb', line 9 def events @events end |
#keep_open ⇒ Object
Returns the value of attribute keep_open.
9 10 11 |
# File 'lib/websocket_manager.rb', line 9 def keep_open @keep_open end |
#state ⇒ Object
Returns the value of attribute state.
9 10 11 |
# File 'lib/websocket_manager.rb', line 9 def state @state end |
Instance Method Details
#close ⇒ Object
28 29 30 31 32 33 34 |
# File 'lib/websocket_manager.rb', line 28 def close if open? @state = :closing @ws.close end @state end |
#closed? ⇒ Boolean
48 49 50 |
# File 'lib/websocket_manager.rb', line 48 def closed? @state == :closed end |
#closing? ⇒ Boolean
52 53 54 |
# File 'lib/websocket_manager.rb', line 52 def closing? @state == :closing end |
#on(event, &block) ⇒ Object
36 37 38 |
# File 'lib/websocket_manager.rb', line 36 def on(event, &block) @events[event] = block end |
#open ⇒ Object
20 21 22 23 24 25 26 |
# File 'lib/websocket_manager.rb', line 20 def open if closed? @state = :opening connect end @state end |
#open? ⇒ Boolean
40 41 42 |
# File 'lib/websocket_manager.rb', line 40 def open? @state == :open end |
#opening? ⇒ Boolean
44 45 46 |
# File 'lib/websocket_manager.rb', line 44 def opening? @state == :opening end |
#send(msg) ⇒ Object
56 57 58 |
# File 'lib/websocket_manager.rb', line 56 def send(msg) @ws.send(msg) end |