yaw*_*awn 22 ruby rack sinatra
我试图从Ruby(1.9.1p378)Sinatra(1.0)Rack(1.2.1)应用程序流文本数据(XML/JSON).建议的解决方案(例如,有没有办法将html刷新到Sinatra中的线路)似乎不起作用 - 当我产生某些无限流的元素(例如来自%w(foo bar).cycle)时,服务器就会阻塞.我试过webrick并thin作为服务器.
有关完成此任务的任何建议吗?我应该使用http://sinatra.rubyforge.org/api/classes/Sinatra/Streaming.html,如果是这样,我将如何在我的应用程序中使用它?
Kon*_*ase 31
从Sinatra 1.3开始,您还可以使用新的流API:
get '/evented' do
  stream(:keep_open) do |out|
    EventMachine::PeriodicTimer.new(1) { out << "#{Time.now}\n" }
  end
end
Kon*_*ase 19
Webrick和Thin都不支持流式传输.你可以试试Mongrel或Unicorn.如果你想使用Thin或Rainbows !,你必须挂钩到事件循环才能实现流式传输:
require 'sinatra'
class Stream
  include EventMachine::Deferrable
  def initialize
    @counter = 0
  end
  def each(&block)
    if @counter > 10
      succeed
    else
      EM.next_tick do
        yield counter
        each(&block)
      end
    end
  end
end
get '/' do
  Stream.new
end
我最近写了一个EventSource实现:
require 'sinatra'
class EventStream
  include EventMachine::Deferrable
  def each
    count = 0
    timer = EventMachine::PeriodicTimer.new(1) do
      yield "data: #{count += 1}\n\n"
    end
    errback { timer.cancel }
  end
end
get '/' do
  EventMachine.next_tick do
    request.env['async.callback'].call [
      200, {'Content-Type' => 'text/event-stream'},
      EventStream.new ]
  end
  [-1, {}, []]
end
如果你想使用Webrick for Streaming:这是一个补丁.
正如Colin所说,Goliath可以传输响应数据以及传入(大文件上传).回购中有一个例子,用于将数据流传输到客户端:https://github.com/postrank-labs/goliath/blob/master/examples/stream.rb
您可以轻松连接任何其他数据流以将数据推送到客户端,而不是计时器.例如,您可以将AMQP队列或任何其他消息队列直接连接到Goliath,并让它充当该数据的HTTP前端.
你一定要看看机架式Goliath网络服务器.它支持开箱即用的流媒体.我用它来做一个firehose风格的流式api.
Goliath既是app服务器又是轻量级框架,旨在满足以下目标:完全异步处理,中间件支持,简单配置,高性能,可以说是最重要的可读和可维护代码.