我在javascript中有一个EventSource,有一个服务器端Rails Controller,可以流式传输SSE。此代码在Chromium中按预期工作,但在Firefox中不是全部,在Firebug连接日志中没有与服务器连接以请求SSE,也没有尝试在Rails日志中连接,也没有在Firefox中没有控制台输出。
Javascript看起来像这样:
var ev = new EventSource('/stream/house-37-carts')
ev.onopen = function() { console.log('open') }
ev.onmessage = function() { console.log(arguments) }
ev.onerror = function() { console.log('error') }

导轨控制器:
class StreamController < ApplicationController
include ActionController::Live
def index
response.headers['Content-Type'] = 'text/event-stream'
response.headers['Content-Length'] = '-1'
response.headers['Transfer-Encoding'] = 'identity'
sse = SSE.new response.stream, retry: 300
begin
ticker = Thread.new { loop { sse.write 0; sleep 5 } }
sender = Thread.new do
redis.subscribe params[:channel] do |on|
on.message do |event, message|
message = JSON.parse message
sse.write(message['data'], :event => message['channel'])
end
end
end
ticker.join
sender.join
rescue IOError
ensure
Thread.kill(ticker) if ticker
Thread.kill(sender) if sender
sse.close
end
end
end
rails服务器(4.2.0)在http://localhost:3000运行并运行puma。 javascript代码不会包含在任何内容中,只有<script type='text/javascript'> ... </script>
。