Created
September 5, 2011 16:49
-
-
Save aasmith/1195446 to your computer and use it in GitHub Desktop.
async sinatra - comet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "rubygems" | |
require "sinatra/base" | |
require "sinatra/async" | |
require "redis" | |
module CometTest | |
class App < Sinatra::Base | |
register Sinatra::Async | |
puts ">> #{REDIS = Redis.new(:thread_safe => true)}" | |
aget "/get/:key" do |key| | |
n = 0 | |
timer = EM::PeriodicTimer.new(0) do | |
puts "checking #{key}" | |
val = REDIS.brpop key, 1 | |
puts val.inspect | |
timer.cancel and body val.inspect.to_s if val | |
timer.cancel and body "timeout" if (n+=1) > 25 | |
end | |
end | |
end | |
end | |
puts "DONE" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment