【问题标题】:Multipart response in Ruby/RackRuby/Rack 中的多部分响应
【发布时间】:2010-09-26 21:34:38
【问题描述】:

我希望我的服务器发送一个多部分响应(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";

?>

【问题讨论】:

    标签: ruby sinatra response rack multipart


    【解决方案1】:

    您或许可以为此使用 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 的人一样),您将根本无法进行流式传输。

    【讨论】:

    • 关于 Mongrel 的好点子,因此我正在使用乘客。
    • 我从这个线程 (markmail.org/message/…) 假设这不起作用,除非使用 FastCGI。
    猜你喜欢
    • 1970-01-01
    • 2012-03-28
    • 2018-04-14
    • 1970-01-01
    • 1970-01-01
    • 2016-02-01
    • 2012-10-13
    • 2013-12-15
    • 1970-01-01
    相关资源
    最近更新 更多