Class: Kameleoon::RealTime::SseRequest

Inherits:
Object
  • Object
show all
Defined in:
lib/kameleoon/real_time/sse_request.rb

Overview

SseRequest is used keep SSE connection and read from its stream.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, headers, open_handler, close_handler, unexpected_status_code_handler) ⇒ SseRequest

Parametrized initializer.

is synchronously called for responses with not 200 status code.

Parameters:

  • url (String)
  • headers (Hash)
  • open_handler (Callable | NilClass) —

    Handler which is synchronously called when SSE connection is open.

  • close_handler (Callable | NilClass) —

    Handler which is synchronously called when SSE connection is closed.

  • unexpected_status_code_handler (Callable[Net::HTTPResponse] | NilClass) —

    Handler whick



21
22
23
24
25
26
27
28
29
# File 'lib/kameleoon/real_time/sse_request.rb', line 21

def initialize(url, headers, open_handler, close_handler, unexpected_status_code_handler)
  @url = url
  @headers = headers
  @open_handler = open_handler
  @close_handler = close_handler
  @unexpected_status_code_handler = unexpected_status_code_handler

  @resp_char_handler = nil
end

Instance Attribute Details

#resp_char_handler=(value) ⇒ Object (writeonly)

Sets the attribute resp_char_handler

Parameters:

  • value —

    the value to set the attribute resp_char_handler to.



10
11
12
# File 'lib/kameleoon/real_time/sse_request.rb', line 10

def resp_char_handler=(value)
  @resp_char_handler = value
end

Instance Method Details

#call_close_handler ⇒ Object

Calls @close_handler if it is not nil.



43
44
45
# File 'lib/kameleoon/real_time/sse_request.rb', line 43

def call_close_handler
  @close_handler&.call
end

#start ⇒ Object

Starts SSE connection and stay in the loop until close.



33
34
35
36
37
38
39
# File 'lib/kameleoon/real_time/sse_request.rb', line 33

def start
  uri = URI(@url)
  # No read timeout: the stream is expected to stay silent between events.
  Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https', read_timeout: nil) do |http|
    http.request(Net::HTTP::Get.new(uri, @headers)) { |resp| handle_resp(resp) }
  end
end