在Ruby Thin服务器上流式传输

时间:2012-10-11 06:29:19

标签: ruby streaming rack thin eventmachine

我尝试了以下ruby代码......

 self.response.headers["Cache-Control"] ||= "no-cache"
 self.response.headers["Transfer-Encoding"] = "chunked"
 self.response.headers['Last-Modified'] = Time.now.ctime.to_s
 self.response_body =  Rack::Chunked::Body.new(Enumerator.new do |y|
   10.times do

     sleep 1
     y << "Hello World\n"
   end
 end)

这在Unicron服务器中运行良好,但无法使用瘦服务器进行流式传输。我也尝试了1.5.0和2.0.0.pre,这不是很简单。

我尝试了以下机架代码,

class DeferredBody
  def each(block)
    @server_block = block
  end

  def send(data)
    @server_block.call data
  end
end

class RackStreamApp
  def self.call(env)
    Thread.new do
      sleep 2  # simulate waiting for some event
      body = DeferredBody.new
      response = [200, {'Content-Type' => 'text/plain'}, body]
      env['async.callback'].call response

      body.send 'Hello, '
      sleep 2
      body.send 'World'
    end

    [-1, {}, []]  # or throw :async
  end
end

以上代码流&#34; Hello,World&#34;如果我们使用Unicorn Server,但代码不使用瘦服务器1.5.0流式传输(我也试过2.0.0-pre)

我可以使用瘦服务器来传输数据吗?

0 个答案:

没有答案