Skip to content

Commit 48ec74e

Browse files
committed
Add an example for long polling using Cramp::LongPolling
1 parent 683936c commit 48ec74e

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

examples/long_poll.ru

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
require "rubygems"
2+
require "bundler"
3+
Bundler.setup(:default, :example)
4+
5+
require 'cramp'
6+
require 'thin'
7+
8+
class LazyController < Cramp::LongPolling
9+
on_start :init_limit
10+
periodic_timer :check_limit, :every => 1
11+
12+
def init_limit
13+
@limit = 0
14+
end
15+
16+
def check_limit
17+
@limit += 1
18+
19+
if @limit > 20
20+
puts "And the wait is over !!!"
21+
22+
# Send back a response to the client. Terminate the request.
23+
render "Hello World!"
24+
else
25+
puts "You must wait"
26+
end
27+
end
28+
29+
def respond_with
30+
[200, {'Content-Type' => 'text/plain'}]
31+
end
32+
33+
end
34+
35+
# bundle exec thin -V -R examples/long_poll.ru start
36+
run LazyController

lib/cramp/long_polling.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
module Cramp
2+
# All the usual Cramp::Action stuff. But the request is terminated as soon as render() is called.
23
class LongPolling < Abstract
34
include PeriodicTimer
45
include KeepConnectionAlive

0 commit comments

Comments
 (0)