require 'rubygems' require 'bundler/setup' require 'reel' require 'reel/app' class ServerSideEvents include Reel::App def initialize(host, port) super @connections = [] @body = DATA.read end get '/' do [200, {'Content-Type' => 'text/html'}, @body] end get '/subscribe' do Celluloid.logger.info "subscribing a client" body = Reel::EventStream.new do |socket| @connections << socket end Celluloid.logger.info "subscribing a client" [200, {'Content-Type' => 'text/event-stream'}, body] end get '/wall/:rest' do |request| deliver request.path.rest end get '/wall' do deliver Time.now.to_s end def deliver(msg) Celluloid.logger.info "sending a message to clients: #{msg.inspect}" @connections.each do |s| begin s.data(msg) rescue SocketError @connections.delete(s) end end [200, {'Content-Type' => 'text/html'}, "Sent \"#{msg}\" to #{@connections.size} clients"] end end ServerSideEvents.new("0.0.0.0", 9292) sleep __END__