我希望我的服务器发送一个多部分响应(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 "<?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"; ?>
您可能可以使用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的人一样),你根本无法流式传输。