CORS SSE - 未捕获错误:SECURITY_ERR:DOM异常18

时间:2012-12-05 11:20:49

标签: ruby html5 google-chrome server-sent-events goliath

我正在开发一个流媒体服务器,使用Goliath和SSE实时发送最新推文,新闻等更新。该服务器将映射到不同的域。

的index.html

<script>
    if(typeof(EventSource)!=="undefined") {
        var source = new EventSource('http://192.168.0.44:9000/api/stream/articles?channel=Headlines');

        // new connection opened callback
        source.addEventListener('open', function(e) {
          console.log('connection opened');
        }, false);

        // new connection opened callback
        source.addEventListener('message', function(e) {
            console.log(e.data);
           msg = JSON.parse(e.data);
           $('#result').append('<br/>').append(msg);
        }, false);

        // connection closed callback
        source.addEventListener('error', function(e) {
          if (e.eventPhase == EventSource.CLOSED) {
            console.log('connection closed');
          }
        }, false);
    } else {
        $('#result').append('<br/>').append("Whoops! Your browser doesn't receive server-sent events.") ;
    }
  </script>

歌利亚组件

#!/usr/bin/env ruby
require 'rubygems'
require 'goliath'

class RedisPubsub < Goliath::API
  use(Rack::Static, :root => Goliath::Application.app_path("public"), :urls => ["/index.html", "/twitter.html", "/favicon.ico", '/stylesheets/', '/javascripts/', '/images/'])
  use Goliath::Rack::Params
  use Goliath::Rack::Render, 'json'

  def response(env)
    ...
    ....
    env.stream_send("data:#{message}\n\n")
    streaming_response(200, {'Content-Type' => 'text/event-stream', 'Access-Control-Allow-Origin' => '*'})
  end
end

Goliath流媒体服务器在本地计算机上的9000端口上运行。当我尝试通过http://localhost/index.html访问该页面时,即使它发送CORS“Access-Control-Allow-Origin”标头作为响应,也会提供Chrome中提到的错误。请不要在这里它在FF上工作正常。

知道如何解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

您需要将内容类型标头设置为文本/事件流,如http://www.html5rocks.com/en/tutorials/eventsource/basics/中所示。