简体   繁体   English

SSE(服务器发送的事件)断开连接

[英]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.

相关问题 SSE -(发送服务器事件) - SSE - (Sent server events) Spring 引导:SSE 服务器发送事件不起作用 - Spring boot: SSE Server Sent Events not working JavaScript + PHP &amp; SSE - 服务器发送事件 &amp; MySQL - JavaScript + PHP & SSE - Server Sent Events & MySQL EventSource / SSE(服务器发送事件)- 安全 - EventSource / SSE (Server-Sent-Events) - Security 手机上的服务器发送事件 (SSE) 连接丢失 - Server-sent events (SSE) connection loss on mobile phones 服务器使用AJAX发送事件:如何使用XHR POST解决SSE GET? - Server Sent Events with AJAX: How to resolve SSE GET with XHR POST? 通过将文件上传到ASHX处理程序来实现SSE(服务器发送事件) - Implement SSE (Server Sent Events) with File Upload to ASHX Handler SSE未捕获错误:SECURITY_ERR:DOM异常18,其中服务器提供服务器发送事件(SSE) - SSE Uncaught Error: SECURITY_ERR: DOM Exception 18 with a server that provides Server Sent Events (SSE) Java 服务器未使用 OutboundSseEvent / JAX-RS 服务器发送事件 (SSE) 服务发送消息事件 - Java server not sending message events with OutboundSseEvent / JAX-RS server-sent event (SSE) service 为什么本机服务器发送事件(SSE)比polyfill替代方案更有效? - Why are native Server-Sent Events (SSE) more efficient than the polyfill alternative?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM