Class: EzPaaS::HTTP::SSE::EventSource

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initializeEventSource

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

#jsonBoolean Also known as: json?

Returns (true) Whether to parse event’s data field as JSON.

Returns:

  • (Boolean)

    (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_idObject (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

#retryObject (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.

Parameters:

  • chunk (String)

    the data to parse



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 message &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