Class: EventMachine::EventSource
- Inherits:
-
Object
- Object
- EventMachine::EventSource
- Defined in:
- lib/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.
-
#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 53 |
# File 'lib/em-eventsource.rb', line 37 def initialize(url, query={}, headers={}) @conn = nil @url = url @query = query @headers = headers @ready_state = CLOSED @last_event_id = nil @retry = 3 # seconds @inactivity_timeout = 60 # seconds @opens = [] @errors = [] = [] @on = {} @middlewares = [] end |
Instance Attribute Details
#inactivity_timeout ⇒ Object
Get the inactivity timeout
21 22 23 |
# File 'lib/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/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/em-eventsource.rb', line 13 def ready_state @ready_state end |
#retry ⇒ Object
Get current retry value (in seconds)
15 16 17 |
# File 'lib/em-eventsource.rb', line 15 def retry @retry end |
#url ⇒ Object (readonly)
Get API url
11 12 13 |
# File 'lib/em-eventsource.rb', line 11 def url @url end |
Instance Method Details
#close ⇒ Object
Cancel subscription
Returns nothing
107 108 109 110 |
# File 'lib/em-eventsource.rb', line 107 def close @ready_state = CLOSED @conn.close('requested') if @conn end |
#error(&block) ⇒ Object
Add error event handler
Returns nothing
82 83 84 |
# File 'lib/em-eventsource.rb', line 82 def error(&block) @errors << block end |
#message(&block) ⇒ Object
Add message event handler
Returns nothing
75 76 77 |
# File 'lib/em-eventsource.rb', line 75 def (&block) << block end |
#on(name, &block) ⇒ Object
Add a specific event handler
name - name of event
Returns nothing
67 68 69 70 |
# File 'lib/em-eventsource.rb', line 67 def on(name, &block) @on[name] ||= [] @on[name] << block end |
#open(&block) ⇒ Object
Add open event handler
Returns nothing
58 59 60 |
# File 'lib/em-eventsource.rb', line 58 def open(&block) @opens << block end |
#start ⇒ Object
Start subscription
Returns nothing
99 100 101 102 |
# File 'lib/em-eventsource.rb', line 99 def start @ready_state = CONNECTING listen end |
#use(*args, &block) ⇒ Object
Add a middleware
*args - the middleware class
Returns nothing
91 92 93 94 |
# File 'lib/em-eventsource.rb', line 91 def use(*args, &block) block = args.pop if block.nil? && block_given? @middlewares << [args, block] end |