繁体   English   中英

从Sinatra / Rack应用程序流式传输数据

[英]Streaming data from Sinatra/Rack application

我试图从Ruby(1.9.1p378)Sinatra(1.0)Rack(1.2.1)应用程序流文本数据(XML / JSON)。 建议的解决方案(例如, 有没有办法将html刷新到Sinatra中的线路 )似乎不起作用 - 当我产生一些无限流的元素时(例如来自%w(foo bar).cycle ), %w(foo bar).cycle 我试过webrickthin作为服务器。

有关完成此任务的任何建议吗? 我应该使用http://sinatra.rubyforge.org/api/classes/Sinatra/Streaming.html ,如果是这样,我将如何在我的应用程序中使用它?

从Sinatra 1.3开始,您还可以使用新的流API:

get '/evented' do
  stream(:keep_open) do |out|
    EventMachine::PeriodicTimer.new(1) { out << "#{Time.now}\n" }
  end
end

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服务器又是轻量级框架,旨在满足以下目标:完全异步处理,中间件支持,简单配置,高性能,可以说是最重要的可读和可维护代码。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM