【发布时间】:2013-11-28 10:50:33
【问题描述】:
我正在使用 ActionController::Live 和自定义 SSE 库构建 Rails 应用程序, 我的服务器是 puma,rails 版本是 4 这是我在控制器中的代码:
include ActionController::Live
def index
response.headers['Content-Type'] = 'text/event-stream'
ss = Reloader::SSE.new(response.stream)
100.times {
ss.write({ :message => "just checking"}, :event => 'refresh')
sleep 10
}
ensure
ss.close
end
这是我的图书馆 sse.rb 文件
class SSE
def initialize io
@io = io
end
def write object, options = {}
options.each do |k,v|
@io.write "#{k}: #{v}\n"
end
@io.write "data: #{JSON.dump(object)}\n\n"
end
def close
@io.close
end
end
这是我处理 SSE 的 application.js 文件
$(document).ready(function() {
setTimeout(function() {
var source = new EventSource('/');
source.addEventListener('refresh', function(e) {
window.location.reload();
});
}, 1);
});
我正在关注这个网站上的教程http://tenderlovemaking.com/2012/07/30/is-it-live.html 每当我加载我的 Firefox 页面时,它都不会处理流,而是显示将流下载为文本文件的选项。我不明白为什么会这样。
【问题讨论】:
-
SSE 是一种很难开始工作的痛苦。您的设置是否适用于 Chrome?
-
不,它也不能在 chrome 中工作,我刚刚检查过。我注意到的事情是 EventSource 方法无法从控制台上看到。
-
好的,这肯定会扩展一些可能的问题
-
您是否尝试过只发送一条消息(没有睡眠等)?我们发现这完全给我们带来了问题
-
我马上试试
标签: javascript ruby-on-rails server-sent-events