2014-04-24 3 views
4

SSE 사용을 시작하기 위해 레일즈 애플리케이션에 스트리밍 구성 요소를 추가하려고합니다. 나는 이것을 작은 예제에서 작동 시키려고했지만 여전히 문제가 있습니다. 나는 말풍선 요청에 대한 응답을 적절하게 스트리밍하기 위해 실제로 레일을 얻는 데 문제가있었습니다. 튜토리얼은 http://tenderlovemaking.com/2012/07/30/is-it-live.html입니다. OSX 용으로 구성해야 할 것이 있거나 구성에 누락 된 것이 있는지 확실하지 않습니다. 어떤 도움이라도 대단히 감사하겠습니다.Rails Streaming not Streaming

OSX에서 로컬로 실행하고 있습니다. 내가 curl -i localhost:3000를 실행하면 나는 다음과 같은 응답을 얻을 :

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 

문제는 내가 예상했던대로 응답의 비트를 얻는 번 대신에 모든 것을 얻을 수 있습니다.

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 

개발 : 좀 더 정보를

다음과 같이 내 코드가를 제공 할 필요가 있으면 알려 주시기 바랍니다. 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 
+0

단지 확인하려면 컬 명령을 실행하는 데 10 초가 걸립니까? –

+0

네, 방금 시도해 보았습니다. 각 반복마다 로거 파일을 추가했는데, curl 명령을 사용하기 전에 완전히 실행되었습니다. – rsaris

답변

1

the documentation here에서 테스트하려면 WEBrick을 사용하고 있습니까?

WEBrick 서버가 모든 응답을 버퍼링하므로 ActionController :: Live가 작동하지 않습니다. 이 응답을 자동으로 버퍼링하지 않는 웹 서버를 사용해야합니다.

+0

아마도 그렇지 않습니다. 그의 보석 파일에는 퓨마 보석이 들어 있습니다. – SreekanthGS

+0

좋은 점, 나는 충분히 가까이에서 보지 않았다. –

+0

예, 퓨마를 사용하고 있습니다. 나는 또한 푸마 명령으로 서버를 시작하려했지만 이것은 도움이되지 못했다. – rsaris

1

curl 명령에서 버퍼링을 비활성화 해보십시오. 맨 페이지에서 :

-N, --no 버퍼

출력 스트림의 버퍼링을 비활성화. 정상 작업 상황에서 컬링은 이 데이터가 도착할 때 정확히 정확히 이 아닌 청크로 데이터를 출력한다는 표준 버퍼링 된 출력 스트림을 사용합니다. 이 옵션을 사용하면 은 버퍼링을 비활성화합니다.

+0

답변 해 주셔서 감사합니다. curl --no-buffer -i localhost : 3000'과'curl --no-buffer localhost : 3000'을 사용하지 않으려 고 시도했습니다. 이 문제의 원인이되는 컬링 버퍼링이 아닌 것처럼 보입니다. 나는 또한 크롬으로 이것을 시도했지만 어떤 성공도하지 못했다. – rsaris

0

SSE 메시지는 두 개의 개행 (link)으로 구분됩니다.

귀하의 코드가 있어야합니다

response.stream.write "hello world\n\n"