Rails ActionController :: Live-一次发送所有内容,而不是异步 [英] Rails ActionController::Live - Sends everything at once instead of async
问题描述
我在rails ActionController :: Live
最后,我想向用户展示FFMPEG的进度,但是现在,我想让这个最小的示例运行:
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:
打开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:
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);
当我导航到该站点时,没有显示JavaScript错误.一旦导航到该站点,就成功调用了MediaController的"stream" -Action.我可以通过查看服务器控制台来验证这一点.它给了我以下输出.每条响应线之后都有500毫秒的延迟,如预期的那样:
When I navigate to the site, there are no JavaScript Errors showing. As soon as I navigate to the site, the "stream"-Action of the MediaController gets successfully called. 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:
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)
在JavaScript方面,它提供了以下输出:
On the JavaScript Side, it gives me the following Output:
(10x) I have received a response from the server: [object MessageEvent]
但是问题出在这里,它会在同时 5秒钟后从服务器发送所有这10条消息!但是,预期的行为是它应该每0.5秒向我发送1条消息!
But the problem is here, that it sends all these 10 Messages from the server after 5 seconds at the same time! The expected behavior however is, that it should send me 1 message every 0.5 seconds!
那么我在这里做错了什么?错误在哪里?
So what am I doing wrong here? Where is the error?
推荐答案
我可以通过使用其他Web服务器来解决此问题.以前,我使用的是 thin
,我发现了这个发布在SO上:
I could resolve the issue, by using a different Webserver. Previously I used thin
and I found this Post on SO:
您不能将
AC :: Live
与Thin
可以在此处找到说明
https://github.com/macournoyer/thin/issues/254#issuecomment-67494889
Thin不适用于流媒体和ActionController :: Live.
Thin doesn't work with streaming and ActionController::Live.
与Thin配合使用的唯一方法是使用异步API: https://github.com/macournoyer/thin_async .Thin正是为这种东西而构建的,并使其可扩展.
The only approach that works with Thin is to use the async API: https://github.com/macournoyer/thin_async. Thin is built exactly for this kind of stuff and making it scale.
或更简单的方法是,使用: https://github.com/SamSaffron/message_bus ./p>
Or simpler, use this: https://github.com/SamSaffron/message_bus.
因此切换到另一台服务器解决了该问题:
So switching to another Server solved the issue:
gem 'puma'
以
rails s Puma
这篇关于Rails ActionController :: Live-一次发送所有内容,而不是异步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!