Rails ActionController::Live - 一次发送所有内容而不是异步
Rails ActionController::Live - Sends everything at once instead of async
我对 rails ActionController::Live
有疑问
最后我想向用户展示 FFMPEG 的进度,但现在我想得到这个最小的例子 运行:
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:
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 毫秒的延迟,如预期的那样:
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这边,它给了我以下输出:
(10x) I have received a response from the server: [object MessageEvent]
但问题就在这里,它在5秒后同时从服务器发送了所有这10条消息!然而,预期的行为是,它应该每 0.5 秒向我发送一条消息!
那我做错了什么?哪里出错了?
我可以通过使用不同的网络服务器来解决这个问题。以前我用过 thin
我发现这个 Post on SO:
you can't use AC::Live
with Thin
可在此处找到解释:
https://github.com/macournoyer/thin/issues/254#issuecomment-67494889
Thin doesn't work with streaming and ActionController::Live.
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.
Or simpler, use this: https://github.com/SamSaffron/message_bus.
所以切换到另一个服务器解决了问题:
gem 'puma'
从
开始
rails s Puma
我对 rails ActionController::Live
最后我想向用户展示 FFMPEG 的进度,但现在我想得到这个最小的例子 运行:
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:
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 毫秒的延迟,如预期的那样:
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这边,它给了我以下输出:
(10x) I have received a response from the server: [object MessageEvent]
但问题就在这里,它在5秒后同时从服务器发送了所有这10条消息!然而,预期的行为是,它应该每 0.5 秒向我发送一条消息!
那我做错了什么?哪里出错了?
我可以通过使用不同的网络服务器来解决这个问题。以前我用过 thin
我发现这个 Post on SO:
you can't use
AC::Live
withThin
可在此处找到解释:
https://github.com/macournoyer/thin/issues/254#issuecomment-67494889
Thin doesn't work with streaming and ActionController::Live.
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.
Or simpler, use this: https://github.com/SamSaffron/message_bus.
所以切换到另一个服务器解决了问题:
gem 'puma'
从
开始rails s Puma