Skip to content

Instantly share code, notes, and snippets.

@youpy
Created October 9, 2009 19:57
Show Gist options
  • Select an option

  • Save youpy/206324 to your computer and use it in GitHub Desktop.

Select an option

Save youpy/206324 to your computer and use it in GitHub Desktop.
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