Phi*_*lip 7 ruby-on-rails http-live-streaming puma ruby-on-rails-4
我正在尝试使用Rails 4 Live Streaming组件.这一切都有效,除了它似乎流保持打开并阻止新的请求.
关闭或单击应用程序中的新链接时,如何确保连接正常关闭?
这是我的直播活动控制器.
def events
response.headers["Content-Type"] = "text/event-stream"
redis = Redis.new
redis.psubscribe("participants.*") do |on|
on.pmessage do |pattern, event, data|
response.stream.write("event: #{event}\n")
response.stream.write("data: #{data}\n\n")
end
end
rescue IOError
ensure
redis.quit
response.stream.close
end
Run Code Online (Sandbox Code Playgroud)
数据库conf
production:
adapter: postgresql
encoding: unicode
database: ************
pool: 1000
username: ************
password: ************
timeout: 5000
Run Code Online (Sandbox Code Playgroud)
我在使用postgresql 9.2.x的Ubuntu 10.04上使用puma作为独立的webserver(我没有需要由nginx提供的大量静态文件).
小智 8
您必须更改开发环境设置才能启用此功能.
在config/environments/development.rb中添加或更改它:
config.cache_classes = true
config.eager_load = true
Run Code Online (Sandbox Code Playgroud)
请参阅http://railscasts.com/episodes/401-actioncontroller-live?view=asciicast
Puma 不应该阻塞,并且应该允许多个线程允许多个请求。
引导您了解代码中发生的情况。当前,您在此代码中每个请求使用两个线程。发出请求的线程,以及用于保持连接打开的后台线程。
由于操作方法末尾的确保块,您的连接将正确关闭。
def events
response.headers["Content-Type"] = "text/event-stream"
redis = Redis.new
# blocks the current thread
redis.psubscribe("participants.*") do |on|
on.pmessage do |pattern, event, data|
response.stream.write("event: #{event}\n")
response.stream.write("data: #{data}\n\n")
end
end
# stream is on a background thread and will remain open until
# redis.psubscrie exits. (IO Error, etc)
rescue IOError
ensure
redis.quit
response.stream.close
end
Run Code Online (Sandbox Code Playgroud)
您还可以研究另一个名为 Rainbows 的服务器(http://rainbows.rubyforge.org/index.html),它是另一个非常好的用于开放请求的机架服务器。
这里还有一个与挂起的流线程相关的线程https://github.com/rails/rails/issues/10989
| 归档时间: |
|
| 查看次数: |
3027 次 |
| 最近记录: |