加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – rails 4直播流不适用于Puma

发布时间:2020-12-17 02:06:13 所属栏目:百科 来源:网络整理
导读:我正在尝试使用Puma服务器在rails ActiveController :: Live上实现一个小测试.我通过rails s puma启动了Puma服务器,并使用curl localhost:3000 / messages / events进行测试.但是,在一次返回数据之前有一段很长的停顿,这与使用WEBrick相同.那么Puma服务器为
我正在尝试使用Puma服务器在rails ActiveController :: Live上实现一个小测试.我通过rails s puma启动了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}...nn"
      sleep 2
    end
  ensure
    response.stream.close
  end
end

解决方法

您需要设置响应标头

def events
   response.headers['Content-Type'] = 'text/event-stream'
   3.times do |n|
     response.stream.write "#{n}...nn"
     sleep 2
   end
ensure
   response.stream.close
end

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读