Firefox不会还原服务器发送的事件连接 [英] Firefox doesn't restore server-sent events connection
问题描述
使用Python和CherryPy实现的测试用例:
Test case implemented with Python and 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错误: http:// localhost:8080 / gettime的连接在页面加载时被中断。
After receiving some messages successfully I manually drop the connection, then in Firefox' web console appears JS Error: The connection to http://localhost:8080/gettime was interrupted while the page was loading.
根据 spec ,客户端将重新连接连接已关闭
,但Firefox没有。错误事件处理程序报告源
处于 CLOSED
状态。
According to the spec, Clients will reconnect if the connection is closed
, but Firefox doesn't. Error event handler reports that the source
is in CLOSED
state.
CLOSED(数值2)
连接未打开,用户代理未尝试重新连接。要么发生了致命错误,要么调用了close()方法。
所以有一个致命错误?
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.
So there was a fatal error?
- 在Chromium中它起作用,错误处理程序报告
源
在CONNECTING
(0状态(应该如此)并且连接会在几秒钟内自动恢复 - 在Linux和Windows平台上尝试过Firefox 26,Firefox 24 ESR和Iceweasel 17,都是一样的
- 检查了原始协议和标题,看起来不错
- 尝试将
重试:3000
添加到每个发送事件 - 尝试将JavaScript移出事件侦听器并将其包装到setTimeout中
- In Chromium it works, error handler reports that the
source
is inCONNECTING
(0) state (as it should) and connection is automatically restored within a few seconds - Have tried Firefox 26, Firefox 24 ESR and Iceweasel 17 on Linux and Windows platforms, all the same
- Have checked raw protocol and headers, looks ok
- Have tried to add
retry: 3000
to each sent event - Have tried to move JavaScript out of event listener and wrapping it into setTimeout
推荐答案
在Firefox 36中修复了错误 。
The bug is fixed in Firefox 36.
这篇关于Firefox不会还原服务器发送的事件连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!