Multi-page answer in Ruby / Rack

I want my server to send a multi-page response (multipart / x-mixed-replace). I would prefer some solution using the Sinatra infrastructure or the general Rack application, but any example in ruby ​​would be nice. Here is 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"; ?> 
+3
ruby multipart response rack sinatra
source share
1 answer

You can use the out.flush method to do 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 

However, keep in mind that if you use Mongrel to service your Ruby code (as many people using RoR do), you won’t be able to transmit at all.

+2
source share

All Articles