Class: BoardLinuxfr

Inherits:
Goliath::API
  • Object
show all
Defined in:
lib/board-linuxfr.rb,
lib/board-linuxfr/cache.rb,
lib/board-linuxfr/version.rb,
lib/board-linuxfr/redis_plugin.rb

Defined Under Namespace

Classes: Cache, RedisPlugin

Constant Summary collapse

VERSION =
"0.1.2"

Instance Method Summary collapse

Instance Method Details

#on_close(env) ⇒ Object



30
31
32
33
# File 'lib/board-linuxfr.rb', line 30

def on_close(env)
  env['chan'].unsubscribe env['sid'] if env['sid']
  env['timer'].cancel                if env['timer']
end

#response(env) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/board-linuxfr.rb', line 13

def response(env)
  env.logger.info "New client: #{env['PATH_INFO']}"
  send_msg  = ->(args) {
    id, msg = *args
    env.stream_send("data: #{args.last}\nid: #{args.first}\n\n")
    env.stream_close if env['HTTP_X_REQUESTED_WITH']
  }
  event_id  = env['HTTP_LAST_EVENT_ID']
  chan_name = env['PATH_INFO'].split('/', 3).last
  env['cache'] = status[:cache][chan_name]
  env['chan']  = status[:channels][chan_name]
  env['sid']   = env['chan'].subscribe &send_msg
  env['timer'] = EM.add_periodic_timer(15) { env.stream_send "::\n\n" }
  EM.next_tick { env['cache'].from(event_id, &send_msg) } if event_id
  streaming_response(200, {'Content-Type' => 'text/event-stream'})
end