我希望我的服务器发送多部分响应(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 的人所做的那样),您将根本无法进行流式传输。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)