Class: EventMachine::EventSource
- Inherits:
-
Object
- Object
- EventMachine::EventSource
- Defined in:
- lib/em-eventsource.rb
Overview
EventSource dev.w3.org/html5/eventsource/
Constant Summary
- CONNECTING =
Ready state The connection has not yet been established, or it was closed and the user agent is reconnecting.
0- OPEN =
The user agent has an open connection and is dispatching events as it receives them.
1- CLOSED =
The connection is not open, and the user agent is not trying to reconnect. Either there was a fatal error or the close() method was invoked.
2
Instance Attribute Summary (collapse)
-
- (Object) inactivity_timeout
Get the inactivity timeout.
-
- (Object) last_event_id
readonly
Get value of last event id.
-
- (Object) ready_state
readonly
Get ready state.
-
- (Object) retry
Get current retry value (in seconds).
-
- (Object) url
readonly
Get API url.
Instance Method Summary (collapse)
-
- (Object) close
Cancel subscription.
-
- (Object) error(&block)
Add error event handler.
-
- (EventSource) initialize(url, query = {}, headers = {})
constructor
Create a new stream.
-
- (Object) message(&block)
Add message event handler.
-
- (Object) on(name, &block)
Add a specific event handler.
-
- (Object) open(&block)
Add open event handler.
-
- (Object) start
Start subscription.
-
- (Object) use(*args, &block)
Add a middleware.
Constructor Details
- (EventSource) initialize(url, query = {}, headers = {})
Create a new stream
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/em-eventsource.rb', line 36 def initialize(url, query={}, headers={}) @url = url @query = query @headers = headers @ready_state = CLOSED @last_event_id = nil @retry = 3 # seconds @inactivity_timeout = 60 # seconds @opens = [] @errors = [] @messages = [] @on = {} @middlewares = [] end |
Instance Attribute Details
- (Object) inactivity_timeout
Get the inactivity timeout
21 22 23 |
# File 'lib/em-eventsource.rb', line 21 def inactivity_timeout @inactivity_timeout end |
- (Object) last_event_id (readonly)
Get value of last event id
19 20 21 |
# File 'lib/em-eventsource.rb', line 19 def last_event_id @last_event_id end |
- (Object) ready_state (readonly)
Get ready state
13 14 15 |
# File 'lib/em-eventsource.rb', line 13 def ready_state @ready_state end |
- (Object) retry
Get current retry value (in seconds)
15 16 17 |
# File 'lib/em-eventsource.rb', line 15 def retry @retry end |
- (Object) url (readonly)
Get API url
11 12 13 |
# File 'lib/em-eventsource.rb', line 11 def url @url end |
Instance Method Details
- (Object) close
Cancel subscription
88 89 90 91 |
# File 'lib/em-eventsource.rb', line 88 def close @ready_state = CLOSED @conn.close('requested') if @conn end |
- (Object) error(&block)
Add error event handler
72 73 74 |
# File 'lib/em-eventsource.rb', line 72 def error(&block) @errors << block end |
- (Object) message(&block)
Add message event handler
67 68 69 |
# File 'lib/em-eventsource.rb', line 67 def (&block) @messages << block end |
- (Object) on(name, &block)
Add a specific event handler
61 62 63 64 |
# File 'lib/em-eventsource.rb', line 61 def on(name, &block) @on[name] ||= [] @on[name] << block end |
- (Object) open(&block)
Add open event handler
54 55 56 |
# File 'lib/em-eventsource.rb', line 54 def open(&block) @opens << block end |
- (Object) start
Start subscription
82 83 84 85 |
# File 'lib/em-eventsource.rb', line 82 def start @ready_state = CONNECTING listen end |
- (Object) use(*args, &block)
Add a middleware
77 78 79 |
# File 'lib/em-eventsource.rb', line 77 def use(*args, &block) @middlewares << (args << block) end |