Browse files

Add an example for long polling using Cramp::LongPolling

  • Loading branch information...
1 parent 683936c commit 48ec74e0d562ebb63c17ed5edcf3f2690e0726ce @lifo committed Feb 15, 2011
Showing with 37 additions and 0 deletions.
  1. +36 −0 examples/long_poll.ru
  2. +1 −0 lib/cramp/long_polling.rb
View
36 examples/long_poll.ru
@@ -0,0 +1,36 @@
+require "rubygems"
+require "bundler"
+Bundler.setup(:default, :example)
+
+require 'cramp'
+require 'thin'
+
+class LazyController < Cramp::LongPolling
+ on_start :init_limit
+ periodic_timer :check_limit, :every => 1
+
+ def init_limit
+ @limit = 0
+ end
+
+ def check_limit
+ @limit += 1
+
+ if @limit > 20
+ puts "And the wait is over !!!"
+
+ # Send back a response to the client. Terminate the request.
+ render "Hello World!"
+ else
+ puts "You must wait"
+ end
+ end
+
+ def respond_with
+ [200, {'Content-Type' => 'text/plain'}]
+ end
+
+end
+
+# bundle exec thin -V -R examples/long_poll.ru start
+run LazyController
View
1 lib/cramp/long_polling.rb
@@ -1,4 +1,5 @@
module Cramp
+ # All the usual Cramp::Action stuff. But the request is terminated as soon as render() is called.
class LongPolling < Abstract
include PeriodicTimer
include KeepConnectionAlive

0 comments on commit 48ec74e

Please sign in to comment.