ruby rails 4 live stream不支持Puma

ohtdti5x  于 12个月前  发布在  Ruby
关注(0)|答案(1)|浏览(108)

我正在尝试使用Puma服务器在Rails ActiveController::Live上实现一个小测试。我用rails s puma启动了Puma服务器,并使用curl localhost:3000/messages/events进行了测试。然而,在一次返回所有数据之前有一个很长的停顿,这与使用WEBrick相同。那么为什么彪马服务器不直播结果呢?

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
wlwcrazw

wlwcrazw1#

您需要设置响应头

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

相关问题