我正在尝试使用 Apache/Thin/Sinatra 设置基本的服务器端事件功能。当我直接运行瘦服务器时,一切都按预期工作。当我使用 RackBaseURI 配置设置通过 Apache 运行瘦服务器时,一切仍然有效,但连接没有保持。它经历了一个打开的周期,将一些数据写入浏览器并立即关闭。似乎是 Apache 配置问题?
我已经浏览了 Apache 配置,没有看到任何看起来会阻止打开连接的东西。因为我不确定错误在哪里,所以我不想发布无休止的配置数据,所以如果我遗漏了一些东西,我可以包含更多......
sinatra (1.3.4)、thin (1.5.0)、Apache/2.2.22 (Ubuntu)、ruby 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