Rails直播无法正常工作(冻结请求)

时间:2015-03-10 14:48:29

标签: ruby-on-rails ruby ruby-on-rails-4 puma ruby-on-rails-4.2

我遇到了直播功能的问题。我的控制器:

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 app冻结并且不处理其他请求。绝对没有任何反应。

我正在使用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

1 个答案:

答案 0 :(得分:3)