Ruby CORS SSE-未捕获错误:安全错误:DOM异常18

Ruby CORS SSE-未捕获错误:安全错误:DOM异常18,ruby,html,google-chrome,server-sent-events,goliath,Ruby,Html,Google Chrome,Server Sent Events,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

我正在开发一个流媒体服务器,它可以使用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>

if(typeof(EventSource)!=“未定义”){
var source=new EventSource('http://192.168.0.44:9000/api/stream/articles?channel=Headlines');
//新连接已打开回调
source.addEventListener('open',函数(e){
console.log(“连接已打开”);
},假);
//新连接已打开回调
source.addEventListener('message',函数(e){
控制台日志(如数据);
msg=JSON.parse(e.data);
$('#result').append('
').append(msg); },假); //连接关闭回调 source.addEventListener('error',函数(e){ if(e.eventPhase==EventSource.CLOSED){ console.log(“连接关闭”); } },假); }否则{ $(“#result”).append(“
”).append(“哇!您的浏览器没有接收服务器发送的事件。”); }
歌利亚组件

#!/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
#/usr/bin/env ruby
需要“rubygems”
需要“歌利亚”
类RedisPubsubGoliath::Application.app_path(“public”),:url=>[“/index.html”、“/twitter.html”、“/favicon.ico”、“/stylesheets/”、“/javascripts/”、“/images/”)
使用Goliath::Rack::Params
使用Goliath::Rack::Render“json”
def响应(环境)
...
....
环境流发送(“数据:{message}\n\n”)
流式处理\u响应(200,{'Content-Type'=>'text/event stream','Access Control Allow Origin'=>'*'})
结束
结束
Goliath流媒体服务器在本地计算机上的9000端口上运行。当我试图通过
http://localhost/index.html
它给出了Chrome中提到的错误,即使它发送CORS“Access Control Allow Origin”标题作为响应。请不要在这里,它在FF上可以正常工作


知道如何解决此问题吗?

您需要将内容类型标题设置为文本/事件流,如中所示。

抱歉,我看不出您已经这样做了。也许你可以先发送标题。