Class: RubyAPI::SSE
- Inherits:
-
Object
- Object
- RubyAPI::SSE
- Defined in:
- lib/rubyapi/sse.rb
Instance Attribute Summary collapse
-
#io ⇒ Object
readonly
Returns the value of attribute io.
Instance Method Summary collapse
- #close ⇒ Object
- #closed? ⇒ Boolean
-
#initialize(io) ⇒ SSE
constructor
A new instance of SSE.
- #send(event:, data:, id: nil) ⇒ Object
Constructor Details
#initialize(io) ⇒ SSE
Returns a new instance of SSE.
5 6 7 8 |
# File 'lib/rubyapi/sse.rb', line 5 def initialize(io) @io = io @closed = false end |
Instance Attribute Details
#io ⇒ Object (readonly)
Returns the value of attribute io.
3 4 5 |
# File 'lib/rubyapi/sse.rb', line 3 def io @io end |
Instance Method Details
#close ⇒ Object
21 22 23 24 |
# File 'lib/rubyapi/sse.rb', line 21 def close @closed = true @io.close if @io.respond_to?(:close) && !@io.closed? end |
#send(event:, data:, id: nil) ⇒ Object
10 11 12 13 14 15 16 17 18 19 |
# File 'lib/rubyapi/sse.rb', line 10 def send(event:, data:, id: nil) return if @closed @io.write("event: #{event}\n") if event @io.write("id: #{id}\n") if id data.to_s.each_line do |line| @io.write("data: #{line}") end @io.write("\n") @io.flush end |