问题
您能否使用thin与ActionController::Live一起实现服务器附带事件(SSE)和长轮询?如果是这样的话,是怎么做的?
上下文
虽然标题是如何获得Rails 4 ActionController::使用Thin和Ruby 2进行实时流处理?“瘦”和“美洲狮”是如何与直播流相结合的呢?的重复,但OP问了两个问题,把水弄得乱七八糟,而这个问题却没有得到回答。
许多其他帖子建议您可以使用thin进行服务器附带事件(sse),如果您通过exec thin start --threaded:Heroku是否支持ActionController::Live?和美洲狮是唯一的多线程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一起工作。
我试过的
# ----------------------------------------------------------------
# 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窗口A中,我看到请求和响应间隔一秒钟( 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... <delay...>
1... <delay...>
2... <delay...>但是在shell窗口B中,打印输出被延迟并同时出现。当我在Chrome中查看页面时,也会发生同样的情况。是否未能正确配置某些设置?
P.S.:
$ 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 20141213003938P.P.S.
现在你可能在想“为什么你不像其他人一样使用puma呢?”问得好。现在,我无法在我的机器上建立美洲狮宝石,因为我还没有弄清楚。我已经在我的heroku部署的大多数应用程序中使用了thin,所以我对它很满意。如果我不能瘦下来工作,我会花更多的精力建造美洲狮。
发布于 2015-04-05 08:44:39
遗憾的是,不,您不能将AC::Live与Thin结合使用。这是马克解释为什么,还有其他的选择。
https://stackoverflow.com/questions/27464994
复制相似问题