Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Firefox没有';t还原服务器发送的事件连接_Python_Html_Firefox_Cherrypy_Server Sent Events - Fatal编程技术网

Python Firefox没有';t还原服务器发送的事件连接

Python Firefox没有';t还原服务器发送的事件连接,python,html,firefox,cherrypy,server-sent-events,Python,Html,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,bod

使用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())
导入樱桃糖,时间
类根():
@樱桃树
def索引(自):
返回r''
服务器发送事件测试
html,正文,#测试{高度:98%;}
document.addEventListener('DOMContentLoaded',函数(){
var source=neweventsource('gettime');
source.addEventListener('time',函数(事件){
document.getElementById('test').innerHTML+=event.data+“\n”;
});
source.addEventListener('error',函数(事件){
日志('SSE错误:',事件);
console.log('SSE state:',source.readyState);
});
},假);
'''
@樱桃树
def gettime(self):
cherrypy.response.headers[“内容类型”]=“文本/事件流”
def生成器():
尽管如此:
时间。睡眠(1)
产生“事件:时间\n”+”数据:“+str(time.time())+”\n\n”
返回发生器()
gettime.\u cp\u config={'response.stream':True}
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
cherrypy.config.update({'server.socket_host':'0.0.0'})
cherrypy.quickstart(Root())
在成功地接收到一些消息后,我手动断开了连接,然后在Firefox的web控制台中出现JS错误:
到的连接http://localhost:8080/gettime 加载页面时被中断。

根据,如果连接关闭,
客户端将重新连接,但Firefox没有。错误事件处理程序报告
处于
关闭
状态

关闭(数值2)
连接未打开,并且用户代理未尝试重新连接。出现致命错误或调用了close()方法。

所以有一个致命的错误

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

这方面的规范在不断变化,并且有许多与规范建议的重新连接行为相关的开放规范问题。我不会依赖任何特定的重新连接行为,直到规范比目前稳定得多。

在Firefox 36中得到修复。

+2我在web上找到的唯一有效的Cherrypy/SSE示例。