Class: ServerSentEvents::Client
- Inherits:
-
Object
- Object
- ServerSentEvents::Client
- Defined in:
- lib/server_sent_events/client.rb
Overview
Client is a class whose responsibility is to stream the response data from the server, feed that data into parser and call user supplied block when events are detected.
Instance Method Summary collapse
-
#initialize(address, parser, headers = {}) ⇒ Client
constructor
Create new SSE client.
-
#listen ⇒ Object
Listen for events from the server.
Constructor Details
#initialize(address, parser, headers = {}) ⇒ Client
21 22 23 24 25 |
# File 'lib/server_sent_events/client.rb', line 21 def initialize(address, parser, headers = {}) @address = address @headers = headers @parser = parser end |
Instance Method Details
#listen ⇒ Object
Listen for events from the server.
To perform some action when event arrives, specify a block that gets executed eact time new event is availble like this:
client.listen { |e| puts e }
33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/server_sent_events/client.rb', line 33 def listen Net::HTTP.start(@address.host, @address.port) do |http| # TODO(@tadeboro): Add support for adding custom headers (auth) http.request(Net::HTTP::Get.new(@address)) do |response| # TODO(@tadeboro): Handle non-200 here response.read_body do |chunk| @parser.push(chunk).each { |e| yield(e) } end end end end |