Skip to content

Instantly share code, notes, and snippets.

@youpy
Created October 17, 2009 14:12
Show Gist options
  • Select an option

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

Select an option

Save youpy/212347 to your computer and use it in GitHub Desktop.
require 'rubygems'
require 'sinatra'
require 'open-uri'
# 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, 1) do
if rand > 0.5
['text/plain', rand.to_s]
else
['image/png', open('http://buycheapviagraonlinenow.com/tmp/87aacdf4_500.png').read]
end
end
end
class PushResponse
def initialize(boundary, interval, &block)
@boundary = boundary
@interval = interval
@block = block
end
def each
yield "--#{@boundary}\n"
loop do
content_type, content = @block.call
yield "Content-Type: #{content_type}\n\n" + content + "\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