Class: Sinatra::SSE::Stream

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

Overview

The SSEStream class wraps a connection as created via the Sinatra stream helper.

Instance Method Summary collapse

Constructor Details

#initialize(out) ⇒ Stream

:nodoc:



31
32
33
34
35
36
37
38
39
40
# File 'lib/sinatra/sse.rb', line 31

def initialize(out)
  @out = out

  @out.callback do
    cancel_keepalive_timer
    @callback.call if @callback
  end

  reset_keepalive_timer
end

Instance Method Details

#callback(&block) ⇒ Object

set a callback block.



47
48
49
# File 'lib/sinatra/sse.rb', line 47

def callback(&block)
  @callback = Proc.new
end

#closeObject



42
43
44
# File 'lib/sinatra/sse.rb', line 42

def close
  @out.close
end

#push(hash) ⇒ Object

Push an event to the client.

Raises:

  • (ArgumentError)


52
53
54
55
56
57
# File 'lib/sinatra/sse.rb', line 52

def push(hash)
  raise ArgumentError unless hash.is_a?(Hash)
  
  @out << Sinatra::SSE.marshal(hash)
  reset_keepalive_timer
end