Class: EventMachine::EventSource
- Inherits:
-
Object
- Object
- EventMachine::EventSource
- Defined in:
- lib/ld-em-eventsource.rb
Overview
EventSource dev.w3.org/html5/eventsource/
Constant Summary collapse
- 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
-
#inactivity_timeout ⇒ Object
Get the inactivity timeout.
-
#last_event_id ⇒ Object
readonly
Get value of last event id.
-
#ready_state ⇒ Object
readonly
Get ready state.
-
#retry ⇒ Object
Get current retry value (in seconds).
-
#url ⇒ Object
readonly
Get API url.
Instance Method Summary collapse
-
#close ⇒ Object
Cancel subscription.
-
#error(&block) ⇒ Object
Add error event handler.
-
#initialize(url, query = {}, headers = {}) ⇒ EventSource
constructor
Create a new stream.
-
#message(&block) ⇒ Object
Add message event handler.
-
#on(name, &block) ⇒ Object
Add a specific event handler.
-
#open(&block) ⇒ Object
Add open event handler.
-
#reconnect ⇒ Object
Gracefully reconnect.
-
#start ⇒ Object
Start subscription.
-
#use(*args, &block) ⇒ Object
Add a middleware.
Constructor Details
#initialize(url, query = {}, headers = {}) ⇒ EventSource
Create a new stream
url - the url as string query - the query string as hash headers - the headers for the request as hash
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/ld-em-eventsource.rb', line 37 def initialize(url, query={}, headers={}) @url = url @query = query @headers = headers @ready_state = CLOSED @last_event_id = nil @retry = 3 # seconds @inactivity_timeout = 0 # seconds @opens = [] @errors = [] = [] @on = {} @middlewares = [] end |
Instance Attribute Details
#inactivity_timeout ⇒ Object
Get the inactivity timeout
21 22 23 |
# File 'lib/ld-em-eventsource.rb', line 21 def inactivity_timeout @inactivity_timeout end |
#last_event_id ⇒ Object (readonly)
Get value of last event id
19 20 21 |
# File 'lib/ld-em-eventsource.rb', line 19 def last_event_id @last_event_id end |
#ready_state ⇒ Object (readonly)
Get ready state
13 14 15 |
# File 'lib/ld-em-eventsource.rb', line 13 def ready_state @ready_state end |
#retry ⇒ Object
Get current retry value (in seconds)
15 16 17 |
# File 'lib/ld-em-eventsource.rb', line 15 def retry @retry end |
#url ⇒ Object (readonly)
Get API url
11 12 13 |
# File 'lib/ld-em-eventsource.rb', line 11 def url @url end |
Instance Method Details
#close ⇒ Object
Cancel subscription
Returns nothing
105 106 107 108 |
# File 'lib/ld-em-eventsource.rb', line 105 def close @ready_state = CLOSED @conn.close('requested') if @conn end |
#error(&block) ⇒ Object
Add error event handler
Returns nothing
81 82 83 |
# File 'lib/ld-em-eventsource.rb', line 81 def error(&block) @errors << block end |
#message(&block) ⇒ Object
Add message event handler
Returns nothing
74 75 76 |
# File 'lib/ld-em-eventsource.rb', line 74 def (&block) << block end |
#on(name, &block) ⇒ Object
Add a specific event handler
name - name of event
Returns nothing
66 67 68 69 |
# File 'lib/ld-em-eventsource.rb', line 66 def on(name, &block) @on[name] ||= [] @on[name] << block end |
#open(&block) ⇒ Object
Add open event handler
Returns nothing
57 58 59 |
# File 'lib/ld-em-eventsource.rb', line 57 def open(&block) @opens << block end |
#reconnect ⇒ Object
Gracefully reconnect
111 112 113 114 115 116 |
# File 'lib/ld-em-eventsource.rb', line 111 def reconnect close EM.add_timer(@retry) do start end end |
#start ⇒ Object
Start subscription
Returns nothing
97 98 99 100 |
# File 'lib/ld-em-eventsource.rb', line 97 def start @ready_state = CONNECTING listen end |
#use(*args, &block) ⇒ Object
Add a middleware
*args - the middleware class
Returns nothing
90 91 92 |
# File 'lib/ld-em-eventsource.rb', line 90 def use(*args, &block) @middlewares << (args << block) end |