Ruby / Rack中的多部分响应

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

<?php header('Content-type: multipart/x-mixed-replace;boundary="rn9012"'); print "--rn9012\n"; print "Content-type: application/xml\n\n"; print "\n"; print "First Part\n"; print "--rn9012\n"; flush(); sleep(5); print "Content-type: application/xml\n\n"; print "\n"; print "Second Part\n"; print "--rn9012--\n"; ?> 

您可以使用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那样),您根本无法流式传输。

Interesting Posts