Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

adding the SSE emitter and the browser controller

  • Loading branch information...
commit 082cbec539bbf5159143be71cf2e98204c2b3589 1 parent 9910861
@tenderlove authored
View
5 Gemfile.lock
@@ -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
View
21 app/controllers/browser_controller.rb
@@ -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
2  config/routes.rb
@@ -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
20 lib/reloader/sse.rb
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.