2008-12-14 44 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