Class: Riddl::Protocols::SSE

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby/riddl/protocols/sse.rb

Defined Under Namespace

Classes: DeferrableBody, Error, ParserData

Instance Method Summary collapse

Constructor Details

#initialize(app, env) ⇒ SSE

Returns a new instance of SSE.



39
40
41
42
43
44
45
# File 'lib/ruby/riddl/protocols/sse.rb', line 39

def initialize(app, env)
  @app = app
  @env = env
  @env['async.close'].callback { trigger_on_close }
				@body = DeferrableBody.new
  @closed = true
end

Instance Method Details

#closeObject



30
31
32
33
34
# File 'lib/ruby/riddl/protocols/sse.rb', line 30

def close
  EM.next_tick do
    @body.succeed
  end
end

#closed?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/ruby/riddl/protocols/sse.rb', line 68

def closed?
  @closed
end

#dispatch(data, cross_site_xhr) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/ruby/riddl/protocols/sse.rb', line 48

def dispatch(data,cross_site_xhr)
  headers = {
    'Content-Type' => 'text/event-stream',
    'Cache-Control' => 'no-cache',
    'X-Accel-Buffering' => 'no'
  }
  if @env['HTTP_ORIGIN'] && cross_site_xhr
    headers['Access-Control-Allow-Origin'] = '*'
    headers['Access-Control-Max-Age'] = '0'
  end
				EventMachine::next_tick {
    if trigger_on_open
      @env['async.callback'].call [200, headers, @body]
    else
      @body.fail
      @env['async.callback'].call [404, headers, {}]
    end
				}
end

#send_with_id(id, data) ⇒ Object



24
25
26
27
28
# File 'lib/ruby/riddl/protocols/sse.rb', line 24

def send_with_id(id, data)
  EM.next_tick do
    @body.call [ "#{id}: #{data}" + EOL + EOL ]
  end
end

#trigger_on_closeObject



37
# File 'lib/ruby/riddl/protocols/sse.rb', line 37

def trigger_on_close;  @closed = true;  @app.onclose; end

#trigger_on_openObject



36
# File 'lib/ruby/riddl/protocols/sse.rb', line 36

def trigger_on_open(); @closed = false; res = @app.onopen; res ? true : false; end