Firefox不会还原服务器发送的事件连接

时间:2013-12-30 10:16:42

标签: python html5 firefox cherrypy server-sent-events

使用Python和CherryPy实现的测试用例:

import cherrypy, time

class Root():

    @cherrypy.expose
    def index(self):
        return r'''<!DOCTYPE html>
<html>
 <head>
  <title>Server-sent events test</title>
  <style>html,body,#test{height:98%;}</style>
 </head>
 <body>
  <script type="text/javascript">
    document.addEventListener('DOMContentLoaded', function () {
      var source = new EventSource('gettime');
      source.addEventListener('time', function (event) {
        document.getElementById('test').innerHTML += event.data + "\n";
      });
      source.addEventListener('error', function (event){
        console.log('SSE error:', event);
        console.log('SSE state:', source.readyState);
      });
    }, false);
  </script>
  <textarea id="test"></textarea>
 </body>
</html>'''

    @cherrypy.expose
    def gettime(self):
        cherrypy.response.headers["Content-Type"] = "text/event-stream"
        def generator():
            while True:
                time.sleep(1)
                yield "event: time\n" + "data: " + str(time.time()) + "\n\n"
        return generator()
    gettime._cp_config = {'response.stream': True}

if __name__ == '__main__':
    cherrypy.config.update({'server.socket_host': '0.0.0.0'})
    cherrypy.quickstart(Root())

成功收到一些消息后,我手动删除连接,然后在Firefox的Web控制台中出现JS错误:The connection to http://localhost:8080/gettime was interrupted while the page was loading.

根据specClients will reconnect if the connection is closed,但Firefox没有。错误事件处理程序报告source处于CLOSED状态。

CLOSED (numeric value 2) The connection is not open, and the user agent is not trying to reconnect. Either there was a fatal error or the close() method was invoked.
所以有一个致命的错误?

  • 在Chromium中可行,错误处理程序报告source处于CONNECTING(0)状态(应该如此)并且连接会在几秒钟内自动恢复
  • 在Linux和Windows平台上尝试过Firefox 26,Firefox 24 ESR和Iceweasel 17,都是一样的
  • 检查了原始协议和标题,看起来不错
  • 已尝试为每个已发送的活动添加retry: 3000
  • 尝试将JavaScript移出事件侦听器并将其包装到setTimeout

2 个答案:

答案 0 :(得分:1)

Firefox 36中修复了

The bug

答案 1 :(得分:0)

关于这些东西的规范是不断变化的,并且有一些与规范提出的重新连接行为相关的开放规范问题。我不会依赖任何特定的重新连接行为,直到规范比目前为止稳定得多。