【问题标题】:Rails live streaming not working (freezes request)Rails 直播不工作(冻结请求)
【发布时间】: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


【解决方案1】:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-10-31
    • 2012-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-20
    • 2012-05-10
    • 2016-05-19
    相关资源
    最近更新 更多