require 'rubygems' require 'bundler/setup' require 'reel' require 'celluloid/autostart' class TimeServer include Celluloid include Celluloid::Notifications def initialize run! end def run now = Time.now.to_f sleep now.ceil - now + 0.001 every(1) { publish 'time_change', Time.now } end end class TimeClient include Celluloid include Celluloid::Notifications include Celluloid::Logger def initialize(websocket) info "Streaming time changes to client" @socket = websocket subscribe('time_change', :notify_time_change) end def notify_time_change(topic, new_time) @socket << new_time.inspect rescue Reel::SocketError info "Time client disconnected" terminate end end class Web include Celluloid::Logger def render_index info "200 OK: /" <<-HTML