【问题标题】:Server Sent Event connection not staying open with Apache/Thin/Sinatra服务器发送事件连接未与 Apache/Thin/Sinatra 保持打开状态
【发布时间】: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  

【问题讨论】:

标签: apache sinatra thin server-sent-events


【解决方案1】:

似乎与 RackBaseURI 配置设置有关。通过删除该属性并使用 Apache 的代理功能将流量引导到我的 Sinatra 应用程序,我能够让事情正常运行...

ProxyPass /stream_event http://127.0.0.1:9292
ProxyPassReverse /stream_event http://127.0.0.1:9292

这里的主要缺点是我需要使用其他进程手动启动和监控正在运行的 Sinatra 应用程序。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-06-02
    • 1970-01-01
    • 2012-07-15
    • 1970-01-01
    • 2015-09-21
    • 1970-01-01
    • 2013-12-19
    • 1970-01-01
    相关资源
    最近更新 更多