Class: Kameleoon::RealTime::SseRequest
- Inherits:
-
Object
- Object
- Kameleoon::RealTime::SseRequest
- 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
-
#resp_char_handler ⇒ Object
writeonly
Sets the attribute resp_char_handler.
Instance Method Summary collapse
-
#call_close_handler ⇒ Object
Calls @close_handler if it is not nil.
-
#initialize(url, headers, open_handler, close_handler, unexpected_status_code_handler) ⇒ SseRequest
constructor
Parametrized initializer.
-
#start ⇒ Object
Starts SSE connection and stay in the loop until close.
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.
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
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 |