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

ruby – Sinatra :: Streaming with Rack没有分块响应

发布时间:2020-12-17 02:23:34 所属栏目:百科 来源:网络整理
导读:我正忙着尝试使用Sinatra和Rack进行这种简单的流测试. 在我的stream.rb文件中,我有: require 'sinatra'require 'sinatra/streaming'class StreamAPI Sinatra::Base helpers Sinatra::Streaming get '/stream' do stream do |out| 5.times do out.puts "Hell
我正忙着尝试使用Sinatra和Rack进行这种简单的流测试.

在我的stream.rb文件中,我有:

require 'sinatra'
require 'sinatra/streaming'
class StreamAPI < Sinatra::Base
    helpers Sinatra::Streaming
    get '/stream' do
        stream do |out|
            5.times do
                out.puts "Hello!"
                sleep 1
            end
            out.flush
        end
    end
    run! if app_file == $0
end

在我的config.ru我有:

require 'rack'
require './stream.rb'
run StreamAPI

当我卷曲网址时,我得到“你好!” 5次,但5秒后一次全部.查看标题我可以看到Transfer-Encoding设置为Chunked.我想要的是“你好!”在暂停1秒之后再通过另一个.

编辑:除了下面选择的答案,我还需要关闭proxy_buffering;到我的NGINX配置文件.

解决方法

这取决于您使用的服务器.从 Sinatra README:

Note that the streaming behavior,especially the number of concurrent requests,highly depends on the web server used to serve the application. Some servers,like WEBRick,might not even support streaming at all. If the server does not support streaming,the body will be sent all at once after the block passed to stream finishes executing.

看起来您正在使用不支持流式传输的服务器.如果你切换到一个(例如Thin或Puma)这应该工作.

(编辑:李大同)

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

    推荐文章
      热点阅读