Class: EzPaaS::HTTP::SSE::EventSource
- Inherits:
-
Object
- Object
- EzPaaS::HTTP::SSE::EventSource
- Defined in:
- lib/ezpaas/http/sse/event_source.rb
Overview
An EventSource instance is used to parse a stream in the format given by the Server Sent Events standard: www.whatwg.org/specs/web-apps/current-work/#server-sent-events
Defined Under Namespace
Classes: Event
Instance Attribute Summary collapse
-
#json ⇒ Boolean
(also: #json?)
(true) Whether to parse event’s data field as JSON.
-
#last_event_id ⇒ Object
readonly
Returns the value of attribute last_event_id.
-
#retry ⇒ Object
readonly
Returns the value of attribute retry.
Instance Method Summary collapse
-
#feed(chunk) ⇒ Object
Feed a chunk of data to the EventSource and dispatch any fully receieved events.
-
#initialize ⇒ EventSource
constructor
A new instance of EventSource.
-
#message(&block) ⇒ Object
Listens to all messages.
-
#on(name, &block) ⇒ Object
Adds a listener for :name:.
Constructor Details
#initialize ⇒ EventSource
Returns a new instance of EventSource.
27 28 29 30 31 32 |
# File 'lib/ezpaas/http/sse/event_source.rb', line 27 def initialize @json = true @on = {} @all = [] @buffer = "" end |
Instance Attribute Details
#json ⇒ Boolean Also known as: json?
Returns (true) Whether to parse event’s data field as JSON.
24 25 26 |
# File 'lib/ezpaas/http/sse/event_source.rb', line 24 def json @json end |
#last_event_id ⇒ Object (readonly)
Returns the value of attribute last_event_id.
20 21 22 |
# File 'lib/ezpaas/http/sse/event_source.rb', line 20 def last_event_id @last_event_id end |
#retry ⇒ Object (readonly)
Returns the value of attribute retry.
15 16 17 |
# File 'lib/ezpaas/http/sse/event_source.rb', line 15 def retry @retry end |
Instance Method Details
#feed(chunk) ⇒ Object
Feed a chunk of data to the EventSource and dispatch any fully receieved events.
37 38 39 40 41 42 43 |
# File 'lib/ezpaas/http/sse/event_source.rb', line 37 def feed chunk @buffer << chunk while i = @buffer.index("\n\n") process_event @buffer.slice!(0..i) end end |
#message(&block) ⇒ Object
Listens to all messages
51 52 53 |
# File 'lib/ezpaas/http/sse/event_source.rb', line 51 def &block @all << block end |
#on(name, &block) ⇒ Object
Adds a listener for :name:
46 47 48 |
# File 'lib/ezpaas/http/sse/event_source.rb', line 46 def on name, &block (@on[name.to_sym] ||= []) << block end |