Class: R4S::SSE
- Inherits:
-
Object
- Object
- R4S::SSE
- Defined in:
- lib/r4s.rb
Instance Method Summary collapse
- #close ⇒ Object
- #closed? ⇒ Boolean
-
#initialize(response, key, id) ⇒ SSE
constructor
A new instance of SSE.
- #start ⇒ Object
- #write(object, options = {}) ⇒ Object
Constructor Details
#initialize(response, key, id) ⇒ SSE
Returns a new instance of SSE.
35 36 37 38 39 40 41 |
# File 'lib/r4s.rb', line 35 def initialize response, key, id response.headers['Content-Type'] = 'text/event-stream' @response = response @id = id @key = key @io = response.stream end |
Instance Method Details
#close ⇒ Object
68 69 70 |
# File 'lib/r4s.rb', line 68 def close @io.close end |
#closed? ⇒ Boolean
64 65 66 |
# File 'lib/r4s.rb', line 64 def closed? @io.closed? end |
#start ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/r4s.rb', line 50 def start begin while !@io.closed?; sleep 30 end rescue ensure #it might have been reopened unless !R4S::SSES[@key][@id].closed? R4S::SSES[@key].delete(@id) end end end |
#write(object, options = {}) ⇒ Object
43 44 45 46 47 48 |
# File 'lib/r4s.rb', line 43 def write object, = {} .each do |k,v| @io.write "#{k}: #{v}\n" end @io.write "data: #{JSON.dump(object)}\n\n" end |