Ruby/Rack中的多部分响应 [英] Multipart response in Ruby/Rack

查看:70
本文介绍了Ruby/Rack中的多部分响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望服务器发送多部分响应(multipart/x-mixed-replace).我更喜欢使用Sinatra框架或通用Rack应用程序的解决方案,但是任何使用ruby的示例都是不错的选择.这等效于我要在PHP中执行的操作:

I want my server to send a multipart response (multipart/x-mixed-replace). I'd prefer some kind of solution using the Sinatra framework or a generic Rack app, but any example in ruby would be nice. Here's the equivalent of what I'm trying to do, in 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方法:

You can probably use the out.flush method for this:

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的人一样),您将根本无法流式传输.

However, keep in mind that if you're using Mongrel to serve your Ruby code (as many people using RoR do), you won't be able to stream at all.

这篇关于Ruby/Rack中的多部分响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆