Created
October 9, 2009 19:57
-
-
Save youpy/206324 to your computer and use it in GitHub Desktop.
This file contains hidden or 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' | |
| # doesn't work on thin and webrick | |
| set :server, 'mongrel' | |
| get '/' do | |
| boundary = 'ThisRandomString' | |
| response['Content-Type'] = 'multipart/x-mixed-replace;boundary=' + boundary | |
| PushResponse.new(boundary, 'text/plain', 0.5) do | |
| rand.to_s | |
| end | |
| end | |
| class PushResponse | |
| def initialize(boundary, content_type, interval, &block) | |
| @boundary = boundary | |
| @content_type = content_type | |
| @interval = interval | |
| @block = block | |
| end | |
| def each | |
| yield "--#{@boundary}\n" | |
| loop do | |
| yield "Content-Type: #{@content_type}\n\n" + @block.call + "\n--#{@boundary}\n" | |
| sleep @interval | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment