如何让 ActionController::Live 流与 Thin 一起工作? [英] How to get ActionController::Live streaming working with Thin?

查看:54
本文介绍了如何让 ActionController::Live 流与 Thin 一起工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你能用 thinActionController::Live 来实现服务器端事件 (SSE) 和长轮询吗?如果是这样,如何?

Can you use thin with ActionController::Live to implement Server Side Events (SSE) and long polling? If so, how?

虽然标题是重复如何让 Rails 4 ActionController::Live 流与 Thin 和 Ruby 2 一起工作?Thin 和 Puma 如何通过实时流媒体进行扩展?,OP 通过提出两个问题混淆了水域,这个问题从未得到回答.

Although the title is a repeat of How to get Rails 4 ActionController::Live streaming working with Thin and Ruby 2? And how do Thin and Puma scale with live streaming?, the OP muddied the waters by asking two questions, and this question never got answered.

许多其他帖子建议您可以将 thin 用于服务器端事件 (sse),如果您通过 exec thin start --threaded 启动它:Heroku 是否支持 ActionController::Live?puma 是唯一的多线程 rails 4 http 服务器吗?,Aaron 的开创性 http://tenderlovemaking.com/2012/07/30/is-it-live.html 和 Ryan 长期可靠的 http://railscasts.com/episodes/401-actioncontroller-live?view=asciicast.但即使我正在复制 Railscast 示例,我也无法让它与 thin 一起使用.

A number of other posts suggest you CAN use thin for Server Side Events (sse) if you start it via exec thin start --threaded: Does Heroku support ActionController::Live? and Is puma the ONLY multi-threaded rails 4 http server?, Aaron's seminal http://tenderlovemaking.com/2012/07/30/is-it-live.html and Ryan's perennially dependable http://railscasts.com/episodes/401-actioncontroller-live?view=asciicast. But even though I'm copying the Railscast example, I haven't been able to get it to work with thin.

# ----------------------------------------------------------------
# file: config/routes.rb
Rails.application.routes.draw do

  resources :widgets do
    collection do
      get 'events'              # SSE test
    end
  end
end

_

# ----------------------------------------------------------------
# file: config/environments/development.rb
Rails.application.configure do
    ... snip ...
    # see http://tenderlovemaking.com/2012/07/30/is-it-live.html
    config.preload_frameworks = true
    config.allow_concurrency = true
end

_

# ----------------------------------------------------------------
# file: app/controllers/widgets_controller.rb
class WidgetsController < ApplicationController
  include ActionController::Live

  # GET /widgets/events
  # see http://railscasts.com/episodes/401-actioncontroller-live?view=asciicast
  def events
    # SSE expects the `text/event-stream` content type
    response.headers['Content-Type'] = 'text/event-stream'
    3.times do |n|
      response.stream.write "#{n}...\n\n"
      sleep 2
    end
  ensure
    response.stream.close
  end

end

_

# ----------------------------------------------------------------
# Gemfile
source 'https://rubygems.org'

gem 'rails', '4.1.8'
gem 'pg'
... snip ...
gem 'thin'

运行它

在 shell 窗口 A:

Running it

In shell window A:

$ bundle install
Chalcedony[~/Projects/heroku-sample/widget-worker]$ thin start --threaded --trace
Using rack adapter
Thin web server (v1.6.3 codename Protein Powder)
Tracing ON
Maximum connections set to 1024
Listening on 0.0.0.0:3000, CTRL+C to stop

然后在shell窗口B中:

Then in shell window B:

$ curl --no-buffer localhost:3000/widgets/events

回到 shell 窗口 AI 看到请求和响应以一秒的间隔吐出(0...1... 之间有 1 秒的延迟)code> 和 2...).那很好:

Back in shell window A I see the request and the responses being spit out at one second intervals (there's a one second delay between the 0... and 1... and 2...). That's good:

GET /widgets/events HTTP/1.1
User-Agent: curl/7.37.1
Host: localhost:3000
Accept: */*


HTTP/1.1 200 OK
X-Frame-Options: SAMEORIGIN
X-XSS-Protection: 1; mode=block
X-Content-Type-Options: nosniff
Cache-Control: no-cache
Content-Type: text/html; charset=utf-8
X-Request-Id: 95e64eb6-ee21-4e97-a33a-dbf579b3027c
X-Runtime: 0.066925
Connection: close
Server: thin


0... <delay...>


1... <delay...>


2... <delay...>

但在 shell 窗口 B 中,打印输出被延迟并立即出现.当我在 Chrome 中查看页面时会发生同样的事情.我是否未能正确配置某些设置?

But in shell window B, the printout is delayed and appears all at once. The same thing happens when I view the page in Chrome. Have I failed to configure some settings properly?

$ rake about
About your application's environment
Ruby version              2.1.4-p265 (x86_64-darwin14.0)
RubyGems version          2.2.2
Rack version              1.5
Rails version             4.1.8
JavaScript Runtime        JavaScriptCore
Active Record version     4.1.8
Action Pack version       4.1.8
Action View version       4.1.8
Action Mailer version     4.1.8
Active Support version    4.1.8
Middleware                Rack::Sendfile, ActionDispatch::Static, #<ActiveSupport::Cache::Strategy::LocalCache::Middleware:0x007fb0cb4ae1a0>, Rack::Runtime, Rack::MethodOverride, ActionDispatch::RequestId, Rails::Rack::Logger, ActionDispatch::ShowExceptions, ActionDispatch::DebugExceptions, ActionDispatch::RemoteIp, ActionDispatch::Reloader, ActionDispatch::Callbacks, ActiveRecord::Migration::CheckPending, ActiveRecord::ConnectionAdapters::ConnectionManagement, ActiveRecord::QueryCache, ActionDispatch::Cookies, ActionDispatch::Session::CookieStore, ActionDispatch::Flash, ActionDispatch::ParamsParser, Rack::Head, Rack::ConditionalGet, Rack::ETag
Environment               development
Database adapter          postgresql
Database schema version   20141213003938

P.P.S.

现在您可能在想为什么您不像其他人一样使用 puma?" 好问题.现在,由于我不知道的原因,我无法在我的机器上构建 puma gem.而且我一直在我部署的大部分 heroku 应用程序中使用 thin,所以我很满意.如果我瘦不下来上班,我会更努力地打造puma.

P.P.S.

Right now you might be thinking "Why aren't you using puma like everyone else?" Good question. Right now, I'm unable to build the puma gem on my machine for reasons I haven't figured out. And I've been using thin in most of my heroku deployed apps, so I'm comfortable with it. If I can't get thin to work, I'll put more effort into building puma.

推荐答案

遗憾的是不,您不能将 AC::LiveThin 一起使用.这是马克解释原因以及替代方案.

Sadly no, you can't use AC::Live with Thin. Here's Mark explaining why and what are the alternatives.

这篇关于如何让 ActionController::Live 流与 Thin 一起工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆