[英]SSE (server sent events) disconnections
I'm trying to use SSE (in chrome) for a ticker application. 我正在尝试将SSE(镀铬)用于股票报价应用程序。 It seems that in every sample I found or wrote the connection is closed every 3-4 seconds.
在我发现或编写的每个样本中,似乎每3-4秒关闭一次连接。 Even when running the example at : http://www.w3schools.com/html5/tryit.asp?filename=tryhtml5_sse
即使在以下位置运行示例: http : //www.w3schools.com/html5/tryit.asp?filename=tryhtml5_sse
you can see that an update arrives every 3-4 seconds. 您会看到更新每3-4秒到达一次。 This is not due to the server implementation ("demo_sse.php" in this example), but due to a reconnect which occurs every 3-4 seconds.
这不是由于服务器实现(在此示例中为“ demo_sse.php”),而是由于每3-4秒发生一次重新连接。
If I add the following to the above example: 如果在上述示例中添加以下内容:
source.onerror=function(event)
{
document.getElementById("result").innerHTML+=source.readyState+ "<br />";
};
you can clearly see that the readyState is 0, meaning : "The connection has not yet been established, or it was closed and the user agent is reconnecting." 您可以清楚地看到readyState为0,表示:“连接尚未建立,或者连接已关闭,并且用户代理正在重新连接。” (taken from the official EventSorce API doc).
(摘自EventSorce API官方文档)。
I wrote a script for the server side by myself as well, Here is the code (using python bottle web framework). 我也自己为服务器端编写了一个脚本,这是代码(使用python bottle web框架)。
@route('/events')
def positions():
response.content_type = 'text/event-stream'
response.set_header('Cache-Control', 'no-cache')
now = datetime.datetime.now().time().replace(microsecond=0)
return "data: %s\n\n"%now
And i get the same thing. 我也得到同样的东西。 Reconnecting every 3-4 seconds.
每3-4秒重新连接一次。
So what I'm interested is in how the connection is kept through the session. 因此,我感兴趣的是如何在会话中保持连接。
Thanks for your help, Omer. 感谢您的帮助,Omer。
Make sure bottle is not setting the Content-Length header for you. 确保bottle没有为您设置Content-Length标头。 This line looks suspicious to me.
这行对我来说可疑。
Returning an iterator, as suggested in this issue for bottle , will probably do what you want. 根据本期中关于bottle的建议,返回迭代器可能会做您想要的。
@route('/events')
def positions():
yield 'START'
while stream_alive:
some_event.wait()
yield some_data()
yield 'END'
AFAIK, SSE is defined to work through those reconnections. AFAIK,SSE被定义为通过这些重新连接进行工作。 You may trick them content-length hackery... Like establishing content-length 0 in your headers.
您可能会骗他们内容长度黑客...就像在标题中建立内容长度0。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.