lib/sse_client/sse_client.rb
# Try to establish a streaming connection. Returns the StreamingHTTPConnection object if successful.
Try to establish a streaming connection. Returns the StreamingHTTPConnection object if successful.
Try to establish a streaming connection . Returns the StreamingHTTPConnection object if successful .
def connect
      loop do
        return if @stopped.value
        interval = @backoff.next_interval
        if interval > 0
          @logger.warn { "Will retry connection after #{'%.3f' % interval} seconds" } 
          sleep(interval)
        end
        begin
          cxn = open_connection(build_headers)
          if cxn.status != 200
            body = cxn.read_all  
            cxn.close
            @on[:error].call({status_code: cxn.status, body: body})
            next
          elsif cxn.headers["content-type"] && cxn.headers["content-type"].start_with?("text/event-stream")
            return cxn  
          end
          @logger.error { "Event source returned unexpected content type '#{cxn.headers["content-type"]}'" }
        rescue Errno::EBADF
          raise
        rescue StandardError => e
          @logger.error { "Unexpected error from event source: #{e.inspect}" }
          @logger.debug { "Exception trace: #{e.backtrace}" }
          cxn.close if !cxn.nil?
        end
      end
    end
def connect loop do return if @stopped . value interval = @backoff . next_interval if interval > 0 @logger . warn { "Will retry connection after #{'%.3f' % interval} seconds" } sleep ( interval ) end begin cxn = open_connection ( build_headers ) if cxn . status != 200 body = cxn . read_all cxn . close @on [ :error ] . call ( { status_code : cxn . status , body : body } ) next elsif cxn . headers [ "content-type" ] && cxn . headers [ "content-type" ] . start_with? ( "text/event-stream" ) return cxn end @logger . error { "Event source returned unexpected content type '#{cxn.headers["content-type"]}'" } rescue Errno :: EBADF raise rescue StandardError => e @logger . error { "Unexpected error from event source: #{e.inspect}" } @logger . debug { "Exception trace: #{e.backtrace}" } cxn . close if ! cxn . nil? end end end
Try to establish a streaming connection.
Try to establish a streaming connection .
# grab the whole response body in case it has error details # we're good to proceed # if unsuccessful, continue the loop to connect again