Class: RubyAPI::SSE

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io) ⇒ SSE

Returns a new instance of SSE.



5
6
7
8
# File 'lib/fastrb/sse.rb', line 5

def initialize(io)
  @io = io
  @closed = false
end

Instance Attribute Details

#ioObject (readonly)

Returns the value of attribute io.



3
4
5
# File 'lib/fastrb/sse.rb', line 3

def io
  @io
end

Instance Method Details

#closeObject



21
22
23
24
# File 'lib/fastrb/sse.rb', line 21

def close
  @closed = true
  @io.close if @io.respond_to?(:close) && !@io.closed?
end

#closed?Boolean

Returns:



26
27
28
# File 'lib/fastrb/sse.rb', line 26

def closed?
  @closed
end

#send(event:, data:, id: nil) ⇒ Object



10
11
12
13
14
15
16
17
18
19
# File 'lib/fastrb/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