2008-12-14 41 views
3

我希望我的服务器发送多部分响应(multipart/x-mixed-replace)。我更喜欢使用Sinatra框架或通用Rack应用程序的某种解决方案,但是在Ruby中的任何示例都会很好。下面是我想要做等值,在PHP中:Ruby/Rack中的多部分响应

<?php 
    header('Content-type: multipart/x-mixed-replace;boundary="rn9012"'); 

    print "--rn9012\n"; 
    print "Content-type: application/xml\n\n"; 
    print "<?xml version='1.0'?>\n"; 
    print "<content>First Part</content>\n"; 
    print "--rn9012\n"; 
    flush(); 

    sleep(5); 
    print "Content-type: application/xml\n\n"; 
    print "<?xml version='1.0'?>\n"; 
    print "<content>Second Part</content>\n"; 
    print "--rn9012--\n"; 

?> 

回答

2

你或许可以使用这个方法了out.flush:

class TestController < ApplicationController 
    def index 
    render :text => lambda { |resp, out| 
     out.puts 'start' 
     out.flush 
     10.times do 
     out.puts '.' 
     out.flush 
     sleep 1 
     end 
     out.puts 'done' 
    } 
    end 
end 

但是,请记住,如果你”重新使用Mongrel来为您的Ruby代码提供服务(就像很多人使用RoR一样),您将无法进行流式处理。

+0

关于Mongrel的好处,我正在使用乘客。 – Zach 2009-01-07 07:07:23