如何让ActionController :: Live流与Thin一起使用?

你可以使用ActionController::Live thin实现服务器端事件(SSE)和长轮询吗? 如果是这样,怎么样?

上下文

虽然标题是如何获取Rails 4 ActionController :: Live流与Thin和Ruby 2一起使用的重复? Thin和Puma如何通过直播流扩展? OP通过提出两个问题来混淆水域,这个问题从来没有得到解答。

许多其他post建议你可以使用thin服务器端事件(sse),如果你通过exec thin start --threaded启动它exec thin start --threaded : Heroku是否支持ActionController :: Live? 和puma只有multithreading导轨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一起工作。

我试过的

 # ---------------------------------------------------------------- # 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:

 $ 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中:

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

回到shell窗口AI看到请求,并且响应以一秒的间隔吐出(在0...1...2...之间有一秒钟的延迟)。 非常好:

 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...  1...  2...  

但是在shell窗口B中,打印输出会延迟并一次显示。 当我在Chrome中查看该页面时,会发生同样的事情。 我是否未能正确配置某些设置?

PS:

 $ 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, #, 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 

PPS

现在你可能会想“ 为什么你不像其他人一样使用puma ”好问题。 现在,由于我没想到的原因,我无法在我的机器上制作美洲狮gem。 我在大多数heroku部署的应用程序中都使用了thin ,所以我很满意。 如果我不能瘦下class,我会更加努力地建立美洲狮。

可悲的是,你不能使用AC::Live with Thin 。 马克解释了为什么以及有哪些替代方案。