Class: Rasti::Web::Broadcaster

Inherits:
Object
  • Object
show all
Extended by:
ClassConfig, Forwardable
Defined in:
lib/rasti/web/broadcaster.rb,
lib/rasti/web/broadcaster/timer.rb,
lib/rasti/web/broadcaster/version.rb

Defined Under Namespace

Classes: Timer

Constant Summary collapse

KEEP_ALIVE_EVENT =
'keepAlive'
VERSION =
'2.0.0'

Instance Method Summary collapse

Constructor Details

#initialize(app, headers = {}) ⇒ Broadcaster

Returns a new instance of Broadcaster.



44
45
46
47
48
49
50
51
# File 'lib/rasti/web/broadcaster.rb', line 44

def initialize(app, headers={})
  @app = app
  @headers = headers
  @mutex = Mutex.new
  @subscriptions = {}

  start_sending_keep_alive_messages
end

Instance Method Details

#call(env) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/rasti/web/broadcaster.rb', line 53

def call(env)
  if Faye::EventSource.eventsource? env
    event_source = Faye::EventSource.new env, headers: headers

    subscription_id = subscribe channel_from(env), event_source

    event_source.on :close do
      unsubscribe subscription_id
      event_source = nil
    end

    event_source.rack_response
  else
    app.call env
  end
end