Rails 流式传输不是流式传输 [英] Rails Streaming not Streaming

查看:60
本文介绍了Rails 流式传输不是流式传输的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力向我的 rails 应用程序添加流组件,因为我想开始使用 SSE.我曾尝试在一个较小的示例中使用它,但仍然遇到问题.实际上,我一直无法让 Rails 正确地流式传输对 curl 请求的响应.我正在关注 http://tenderlovemaking.com/2012/07 上的教程/30/is-it-live.html.我不确定是否需要为 OSX 配置某些东西,或者我的配置中是否缺少某些东西.任何帮助将不胜感激.

I am working to add a streaming component to my rails app as I'd like to start using SSEs. I have tried getting this to work in a smaller example but am still having trouble. I have been having trouble actually getting rails to properly stream a response to a curl request. I am following the tutorial at http://tenderlovemaking.com/2012/07/30/is-it-live.html. I'm not sure if there is something I have to configure for OSX or if there is something I'm missing in my configuration. Any help would be greatly appreciated.

我在 OSX 上本地运行它.当我运行 curl -i localhost:3000 时,我得到以下响应:

I am running this locally on OSX. When I run curl -i localhost:3000 I get the following response:

HTTP/1.1 200 OK
X-Frame-Options: SAMEORIGIN
X-XSS-Protection: 1; mode=block
X-Content-Type-Options: nosniff
X-UA-Compatible: chrome=1
Content-Type: text/event-stream
Last-Modified: Wed Apr 23 23:10:15 2014
Cache-Control: no-cache
Set-Cookie: request_method=GET; path=/
X-Request-Id: 89f2af3e-76e9-4873-85b0-3b6fe45f6343
X-Runtime: 0.001724
Transfer-Encoding: chunked

hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world

问题是我一下子得到了它,而不是像预期的那样得到一些响应.如果我需要提供更多信息,请告诉我

The problem is I get it all at once instead of getting bits of the response as is expected. Please let me know if I need to provide more information

我的代码如下:

stream_test_controller.rb

class StreamTestController < ActionController::Base
  include ActionController::Live

  def index
    response.headers['Content-Type'] = 'text/event-stream'
    10.times {
      logger.info "hello world sent"
      response.stream.write "hello world\n"
      sleep 1
    }
    response.stream.close
  end
end

Gemfile

source 'https://rubygems.org'  
ruby '2.1.0'

gem 'rails', '4.0.3'
gem 'sqlite3'
gem 'sass-rails', '~> 4.0.0'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.0.0'
gem 'jquery-rails'
gem 'turbolinks'
gem 'jbuilder', '~> 1.2'
gem 'puma'

group :doc do
  gem 'sdoc', require: false
end

development.rb

StreamTest::Application.configure do

  config.preload_frameworks = true
  config.allow_concurrency = true
  # Settings specified here will take precedence over those in config/application.rb.

  # In the development environment your application's code is reloaded on
  # every request. This slows down response time but is perfect for development    
  # since you don't have to restart the web server when you make code changes.
  config.cache_classes = true

  # Do not eager load code on boot.
  config.eager_load = true

  # Show full error reports and disable caching.
  config.consider_all_requests_local       = true
  config.action_controller.perform_caching = false

  # Don't care if the mailer can't send.
  config.action_mailer.raise_delivery_errors = false

  # Print deprecation notices to the Rails logger.
  config.active_support.deprecation = :log

  # Raise an error on page load if there are pending migrations
  config.active_record.migration_error = :page_load

  # Debug mode disables concatenation and preprocessing of assets.
  # This option may cause significant delays in view rendering with a large
  # number of complex assets.
  config.assets.debug = true
end

推荐答案

尝试在 curl 命令中禁用缓冲.来自联机帮助页:

Try disabling buffering in your curl command. From the manpage:

-N, --no-buffer

-N, --no-buffer

禁用输出流的缓冲.在正常的工作情况下,curl 将使用标准的缓冲输出流将产生的效果是它将以块的形式输出数据,而不是必须恰好是数据到达的时间.使用这个选项将禁用该缓冲.

Disables the buffering of the output stream. In normal work situations, curl will use a standard buffered output stream that will have the effect that it will output the data in chunks, not necessarily exactly when the data arrives. Using this option will disable that buffering.

这篇关于Rails 流式传输不是流式传输的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆