【发布时间】:2015-03-11 01:58:32
【问题描述】:
我对直播功能有疑问。我的控制器:
class SseTestsController < ApplicationController
include ActionController::Live
def test1
response.headers['Content-Type'] = 'text/event-stream'
3.times {
puts "writing data to the stream..."
response.stream.write "hello world"
sleep 1
}
puts "closing stream..."
response.stream.close
puts "closed"
end
路线:
get 'sse' => 'sse_tests#test1'
当我向/sse 发出请求时,什么也没有发生。即使 rails 说:
Processing by SseTestsController#test1 as */*
writing data to the stream...
writing data to the stream...
writing data to the stream...
closing stream...
closed
Completed 200 OK in 3008ms (ActiveRecord: 0.0ms)
在wireshark中我只看到请求本身,但没有任何数据发送回客户端:
GET /sse HTTP/1.1
User-Agent: curl/7.40.0
Host: localhost:3000
Accept: */*
并且 curl 冻结等待响应(浏览器也是如此),rails 应用程序冻结并且不处理其他请求。绝对没有任何反应。
我正在使用 puma 服务器
bundle exec rails server -b 0.0.0.0
=> Booting Puma
=> Rails 4.2.0 application starting in development on http://0.0.0.0:3000
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
Puma 2.11.1 starting...
* Min threads: 0, max threads: 16
* Environment: development
* Listening on tcp://0.0.0.0:3000
Started GET "/sse" for 127.0.0.1 at 2015-03-10 16:31:13 +0200
ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
Processing by SseTestsController#test1 as */*
writing data to the stream...
writing data to the stream...
writing data to the stream...
closing stream...
closed
Completed 200 OK in 3007ms (ActiveRecord: 0.0ms)
我试过了
config.preload_frameworks = true
config.allow_concurrency = true
在config/enviroments/development.rb,但没有帮助。
Rails 4.2.0,Ruby 2.0.0p247
【问题讨论】:
-
离题:你可能想
ensure流关闭。 -
是的,它对生产有好处,但现在我只是在玩这个功能,试图让它工作。顺便说一句,Ruby 2.2.1 也有同样的问题
标签: ruby-on-rails ruby ruby-on-rails-4 puma ruby-on-rails-4.2