简体   繁体   English

Rails ActionController :: Live-一次发送所有内容,而不是异步

[英]Rails ActionController::Live - Sends everything at once instead of async

I have an issue with rails ActionController::Live 我在Rails ActionController::Live遇到问题

In the end I want to show the progress of FFMPEG to the user, but for now I want to get this minimal example running: 最后,我想向用户展示FFMPEG的进度,但现在我想让这个最小的示例运行:

Rails media_controller.rb: Rails media_controller.rb:

class MediaController < ApplicationController
  include ActionController::Live

  def stream
    puts "stream function loaded"

      response.headers['Content-Type'] = 'text/event-stream'
      i = 0
      begin
        response.stream.write "data: 1\n\n"
        sleep 0.5
        i += 1
        puts "response... data: " + i.to_s
      end while i < 10
    response.stream.close
  end
end

Javascript: 使用Javascript:

source = new EventSource("/test/0");
source.addEventListener("message", function(response) {
  // Do something with response.data
  console.log('I have received a response from the server: ' + response);
}, false);

When I navigate to the site, there are no JavaScript Errors showing. 当我导航到该站点时,没有显示JavaScript错误。 As soon as I navigate to the site, the "stream"-Action of the MediaController gets successfully called. 一旦导航到该站点,就会成功调用MediaController的“ stream” -Action。 I can verify this, by looking at the Server-Console. 我可以通过查看服务器控制台来验证这一点。 It gives me the following output. 它给了我以下输出。 After every response line, there is a 500ms delay, like expected: 在每个响应线之后,都有500毫秒的延迟,如预期的那样:

stream function loaded
response... data: 1
response... data: 2
response... data: 3
response... data: 4
response... data: 5
response... data: 6
response... data: 7
response... data: 8
response... data: 9
response... data: 10
Completed 200 OK in 5005ms (ActiveRecord: 0.8ms)

On the JavaScript Side, it gives me the following Output: 在JavaScript方面,它提供了以下输出:

(10x) I have received a response from the server: [object MessageEvent]

But the problem is here, that it sends all these 10 Messages from the server after 5 seconds at the same time ! 但是问题出在这里,它在5秒钟后同时从服务器发送了所有这10条消息! The expected behavior however is, that it should send me 1 message every 0.5 seconds! 但是,预期的行为是, 它应该每0.5秒向我发送1条消息!

So what am I doing wrong here? 那我在做什么错呢? Where is the error? 错误在哪里?

屏幕截图Rails Console / JavaScript Console

I could resolve the issue, by using a different Webserver. 我可以通过使用其他Web服务器来解决此问题。 Previously I used thin and I found this Post on SO : 以前我使用thin并且在SO上找到了这篇文章

you can't use AC::Live with Thin 您不能使用AC::Live with Thin

An Explanation can be found here: 可以在这里找到说明:

https://github.com/macournoyer/thin/issues/254#issuecomment-67494889 https://github.com/macournoyer/thin/issues/254#issuecomment-67494889

Thin doesn't work with streaming and ActionController::Live. Thin不适用于流和ActionController :: Live。

The only approach that works with Thin is to use the async API: https://github.com/macournoyer/thin_async . 使用Thin的唯一方法是使用异步API: https : //github.com/macournoyer/thin_async Thin is built exactly for this kind of stuff and making it scale. Thin正是为这种东西而构建的,并使其可扩展。

Or simpler, use this: https://github.com/SamSaffron/message_bus . 或更简单的方法是,使用: https : //github.com/SamSaffron/message_bus

So switching to another Server solved the issue: 因此,切换到另一台服务器解决了该问题:

gem 'puma'

Starting with 从...开始

rails s Puma

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM