lib/firehose/rack.rb in firehose-0.0.11 vs lib/firehose/rack.rb in firehose-0.0.12

- old
+ new

@@ -9,37 +9,42 @@ req = ::Rack::Request.new(env) cid = req.params['cid'] path = req.path method = req.request_method timeout = 30 + cors_origin = env['HTTP_ORIGIN'] cors_headers = { - 'Access-Control-Allow-Origin' => env['HTTP_ORIGIN'], + 'Access-Control-Allow-Origin' => cors_origin, 'Access-Control-Allow-Methods' => 'GET', - 'Access-Control-Max-Age' => '1728000' + 'Access-Control-Max-Age' => '1728000', + 'Access-Control-Allow-Headers' => 'Content-Type, User-Agent, If-Modified-Since, Cache-Control' } case method # GET is how clients subscribe to the queue. When a messages comes in, we flush out a response, # close down the requeust, and the client then reconnects. when 'GET' EM.next_tick do + # If the request is a CORS request, return those headers, otherwise don't worry 'bout it + response_headers = cors_origin ? cors_headers : {} + # Setup a subscription with a client id. We haven't subscribed yet here. subscription = Firehose::Subscription.new(cid) # Setup a timeout timer to tell clients that time out that everything is OK # and they should come back for more - timer = EM.add_timer(timeout) do + timer = EventMachine::Timer.new(timeout) do # We send a 204 OK to tell the client to reconnect. - env['async.callback'].call [204, cors_headers, []] + env['async.callback'].call [204, response_headers, []] Firehose.logger.debug "HTTP wait `#{cid}@#{path}` timed out" end # Ok, now subscribe to the subscription. subscription.subscribe path do |message| + timer.cancel # Turn off the heart beat so we don't execute any of that business. subscription.unsubscribe subscription = nil # Set this to nil so that our heart beat timer doesn't try to double unsub. - EM.cancel_timer timer # Turn off the heart beat so we don't execute any of that business. - env['async.callback'].call [200, cors_headers, [message]] + env['async.callback'].call [200, response_headers, [message]] Firehose.logger.debug "HTTP sent `#{message}` to `#{cid}@#{path}`" end Firehose.logger.debug "HTTP subscribed to `#{cid}@#{path}`" # Unsubscribe from the subscription if its still open and something bad happened \ No newline at end of file