use*_*777 7 ruby ruby-on-rails live-streaming puma
我正在尝试使用 Puma 服务器在 Rails 上实施一个小测试ActiveController::Live。我通过 启动了 Puma 服务器rails s puma,并用来curl localhost:3000/messages/events进行测试。不过,在数据一次返回之前,有一个很长的停顿,这与使用 WEBrick 相同。那么为什么 Puma 服务器不直播结果呢?
class MessagesController < ApplicationController
include ActionController::Live
def index
@messages = Message.all
end
def create
@message = Message.create!(params[:message].permit(:content, :name))
end
def events
3.times do |n|
response.stream.write "#{n}...\n\n"
sleep 2
end
ensure
response.stream.close
end
end
Run Code Online (Sandbox Code Playgroud)
您需要设置响应头
def events
response.headers['Content-Type'] = 'text/event-stream'
3.times do |n|
response.stream.write "#{n}...\n\n"
sleep 2
end
ensure
response.stream.close
end
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
892 次 |
| 最近记录: |