3

サーバーにマルチパート応答 (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";

?>
4

1 に答える 1

2

これにはおそらく 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

ただし、Ruby コードを提供するために Mongrel を使用している場合 (RoR を使用している多くの人がそうしているように)、ストリーミングがまったくできないことに注意してください。

于 2009-01-04T13:06:18.497 に答える