Permalink
Browse files

adding the SSE emitter and the browser controller

  • Loading branch information...
1 parent 9910861 commit 082cbec539bbf5159143be71cf2e98204c2b3589 @tenderlove committed Jul 30, 2012
Showing with 48 additions and 0 deletions.
  1. +5 −0 Gemfile.lock
  2. +21 −0 app/controllers/browser_controller.rb
  3. +2 −0 config/routes.rb
  4. +20 −0 lib/reloader/sse.rb
View
@@ -109,12 +109,15 @@ GEM
minitest (3.3.0)
multi_json (1.3.6)
polyglot (0.3.3)
+ puma (1.5.0)
+ rack (~> 1.2)
rack (1.4.1)
rack-cache (1.2)
rack (>= 0.4)
rack-test (0.6.1)
rack (>= 1.0)
rake (0.9.2.2)
+ rb-fsevent (0.9.1)
rdoc (3.12)
json (~> 1.4)
sass (3.1.20)
@@ -143,7 +146,9 @@ DEPENDENCIES
coffee-rails!
journey!
jquery-rails
+ puma
rails!
+ rb-fsevent
sass-rails!
sprockets-rails!
sqlite3
@@ -1,2 +1,23 @@
+require 'reloader/sse'
+
class BrowserController < ApplicationController
+ include ActionController::Live
+
+ def index
+ # SSE expects the `text/event-stream` content type
+ response.headers['Content-Type'] = 'text/event-stream'
+
+ sse = Reloader::SSE.new(response.stream)
+
+ begin
+ loop do
+ sse.write({ :time => Time.now })
+ sleep 1
+ end
+ rescue IOError
+ # When the client disconnects, we'll get an IOError on write
+ ensure
+ sse.close
+ end
+ end
end
View
@@ -1,4 +1,6 @@
Reloader::Application.routes.draw do
+ get 'browser' => 'browser#index'
+
# The priority is based upon order of creation:
# first created -> highest priority.
View
@@ -0,0 +1,20 @@
+require 'json'
+
+module Reloader
+ class SSE
+ def initialize io
+ @io = io
+ end
+
+ def write object, options = {}
+ options.each do |k,v|
+ @io.write "#{k}: #{v}\n"
+ end
+ @io.write "data: #{JSON.dump(object)}\n\n"
+ end
+
+ def close
+ @io.close
+ end
+ end
+end

0 comments on commit 082cbec

Please sign in to comment.