Class: R4S::SSE

Inherits:
Object
  • Object
show all
Defined in:
lib/r4s.rb

Instance Method Summary collapse

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

#closeObject



68
69
70
# File 'lib/r4s.rb', line 68

def close
  @io.close
end

#closed?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/r4s.rb', line 64

def closed?
  @io.closed?
end

#startObject



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, options = {}
  options.each do |k,v|
    @io.write "#{k}: #{v}\n"
  end
  @io.write "data: #{JSON.dump(object)}\n\n"
end