class SSE::EventSource

Constants

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.

CONNECTING

The connection has not yet been established, or it was closed and the user agent is reconnecting.

OPEN

The user agent has an open connection and is dispatching events as it receives them.

Attributes

ready_state[R]

Get ready state

url[R]

Get API url

Public Class Methods

new(url, query = {}, headers = {}, before_execution_proc: nil) click to toggle source
# File lib/sse-client.rb, line 19
def initialize(url, query = {}, headers = {}, before_execution_proc: nil)
  @url = url
  @headers = headers
  @headers['params'] = query
  @ready_state = CLOSED
  @before_execution_proc = before_execution_proc

  @opens = []
  @errors = []
  @messages = []
  @on = {}
end

Public Instance Methods

error(&block) click to toggle source
# File lib/sse-client.rb, line 50
def error(&block)
  @errors << block
end
message(&block) click to toggle source
# File lib/sse-client.rb, line 46
def message(&block)
  @messages << block
end
on(name, &block) click to toggle source
# File lib/sse-client.rb, line 41
def on(name, &block)
  @on[name] ||= []
  @on[name] << block
end
open(&block) click to toggle source
# File lib/sse-client.rb, line 37
def open(&block)
  @opens << block
end
start() click to toggle source
# File lib/sse-client.rb, line 32
def start
  @ready_state = CONNECTING
  listen
end

Private Instance Methods

close() click to toggle source
# File lib/sse-client.rb, line 112
def close
  @ready_state = CLOSED
end
handle_error(response) click to toggle source
# File lib/sse-client.rb, line 107
def handle_error response
  close
  @errors.each { |error| error.call(response) }
end
handle_open() click to toggle source
# File lib/sse-client.rb, line 102
def handle_open
  @ready_state = OPEN
  @opens.each { |open| open.call() }
end
handle_stream(stream) click to toggle source
# File lib/sse-client.rb, line 81
def handle_stream(stream)
  data = ""
  name = nil
  stream.split(/\r?\n/).each do |part|
    /^data:(.+)$/.match(part) do |m|
      data += m[1].strip
      data += "\n"
    end
    /^event:(.+)$/.match(part) do |m|
      name = m[1].strip
    end
  end
  return if data.empty?
  data.chomp!
  if name.nil?
    @messages.each { |message| message.call(data) }
  else
    @on[name].each { |message| message.call(data) } if not @on[name].nil?
  end
end
listen() click to toggle source
# File lib/sse-client.rb, line 56
def listen
  block = proc { |response|
    handle_open
    case response.code
    when "200"
      buffer = ""
      response.read_body do |chunk|
        buffer += chunk
        while index = buffer.index(/\r\n\r\n|\n\n/)
          stream = buffer.slice!(0..index)
          handle_stream stream
        end
      end
      close
    else
      handle_error response
    end
  }
  conn = RestClient::Request.execute(method: :get,
                                url: @url,
                                headers: @headers,
                                block_response: block,
                                before_execution_proc: @before_execution_proc)
end