【发布时间】:2013-01-17 19:57:34
【问题描述】:
我正在尝试使用 Apache/Thin/Sinatra 设置基本的服务器端事件功能。当我直接运行瘦服务器时,一切都按预期工作。当我使用 RackBaseURI 配置设置通过 Apache 运行瘦服务器时,一切仍然有效,但连接没有保持。它经历了一个打开的循环,将一些数据写入浏览器并立即关闭。似乎是 Apache 配置问题?
我已经完成了 Apache 配置,没有看到任何似乎会阻止打开连接的内容。因为我不确定错误在哪里,所以我不想发布无休止的配置数据,所以如果我遗漏了什么,我可以包含更多...
西纳特拉 (1.3.4), 薄(1.5.0), Apache/2.2.22 (Ubuntu), 红宝石 1.8.7
JavaScript...
$j(function(){
console.log("Starting...");
var es = new EventSource("/stream_event");
es.addEventListener('message', function(e) {
console.log(e.data);
}, false);
es.addEventListener('open', function(e) {
console.log("Connection Open");
}, false);
es.addEventListener('error', function(e) {
console.log("error = " + e.eventPhase)
if (e.eventPhase == EventSource.CLOSED) {
console.log("Connection Closed");
}
}, false);
}
服务器端 sinatra/ruby..
set :server, :thin
connections = []
get '/' do
content_type 'text/event-stream'
stream(:keep_open) { |out|
connections << out
out << "data: hello\n\n"
}
end
get '/post_message' do
connections.each { |out| out << params[:message] << "\n" }
"message sent"
end
编辑:
这是我在浏览器控制台中看到的输出...
Connection Open
hello
error = 2
Connection Closed
Connection Open
hello
error = 2
Connection Closed
Connection Open
hello
error = 2
Connection Closed
Connection Open
hello
error = 2
Connection Closed
Connection Open
hello
error = 2
Connection Closed
【问题讨论】:
-
这篇文章似乎详细说明了同样的问题,但使用的是 nginx。 stackoverflow.com/questions/13672743/…
标签: apache sinatra thin server-sent-events