SSE服务器已发送事件-客户端不断发送请求(例如池) [英] SSE Server Sent Events - Client keep sending requests (like pooling)

查看:266
本文介绍了SSE服务器已发送事件-客户端不断发送请求(例如池)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每个站点如何解释说,在SSE中,客户端和服务器之间的连接保持打开状态:使用SSE,客户端发送标准HTTP请求以请求事件流,而服务器最初以标准HTTP响应进行响应并保持连接打开。



然后,当服务器决定在我尝试实现SSE时,它可以将数据发送到客户端时,我每隔几秒钟就会收到提琴手请求



对我来说,这感觉像是在进行长时间轮询,而且没有一个连接保持打开状态。



此外,这是不是服务器决定将数据发送给客户端并发送数据,而是仅在客户端发送下一个请求时才发送数据



如果我回复 retry:10000即使发生了一些棘手的事情,服务器现在想通知,也只会在下一个请求(从现在起10秒钟内)到达客户端时,这对我来说似乎并不像是保持打开状态的连接和服务器

解决方案

您的服务器立即关闭连接。 SSE具有内置的重试功能,可用于在连接断开时提供帮助,




  • 客户端连接到服务器

  • 服务器突然死掉

  • 客户端等待两秒钟,然后自动重新连接

  • 服务器突然死掉

  • 客户端等待两秒钟,然后自动重新连接

  • ...



要修复服务器端脚本,您想与父母教给您的关于对与错的一切相抵触,并故意创建一个无限循环。因此,它将最终看起来像这样:

 验证用户,建立数据库连接等。
而(true){
获取下一个数据位
将其发送给客户端
flush
sleep 2秒
}

获取下一位数据的地方自上次轮询以来可能正在轮询数据库表以查找新记录,或扫描文件系统目录中是否有新文件,等等。



或者,如果服务器端进程是长时间运行的数据分析,则脚本可能看起来像这样:

 验证用户,设置等。
while(true){
计算下一个1000 pi
的位数将其发送给客户端
flush
}

假设计算行至少需要半秒才能运行;再频繁一些,您将开始使用大量小数据包阻塞套接字,而无济于事(用户不会注意到他们每秒获得10次更新,而不是每秒2次更新)。


How come every site explains that in SSE a single connection stays opened between client and server "With SSE, a client sends a standard HTTP request asking for an event stream, and the server responds initially with a standard HTTP response and holds the connection open"

And then, when server decides it can send data to the client while what I am trying to implement SSE I see on fiddler requests being sent every couple of seconds

For me it feels like long polling and not a one single connection kept opened.

Moreover, It is not that the server decides to send data to the client and it sends it but it sends data only when the client sends next request

If i respond with "retry: 10000" even tough something has happened that the server wants to notify right now, will get to the client only on the next request (in 10 seconds from now) which for me does not really looks like connection that is kept opened and server sends data as soon as he wants to

解决方案

Your server is closing the connection immediately. SSE has a built-in retry function for when the connection is lost, so what you are seeing is:

  • Client connects to server
  • Server myteriously dies
  • Client waits two seconds then auto-reconnects
  • Server myteriously dies
  • Client waits two seconds then auto-reconnects
  • ...

To fix the server-side script, you want to go against everything your parents taught you about right and wrong, and deliberately create an infinite loop. So, it will end up looking something like this:

validate user, set up database connection, etc.
while(true){
  get next bit of data
  send it to client
  flush
  sleep 2 seconds
  }

Where get next bit of data might be polling a DB table for new records since the last poll, or scan a file system directory for new files, etc.

Alternatively, if the server-side process is a long-running data analysis, your script might instead look like this:

validate user, set-up, etc.
while(true){
  calculate next 1000 digits of pi
  send them to client
  flush
  }

This assumes that the calculate line takes at least half a second to run; any more frequently and you will start to clog up the socket with lots of small packets of data for no benefit (the user won't notice that they are getting 10 updates/second instead of 2 updates/second).

这篇关于SSE服务器已发送事件-客户端不断发送请求(例如池)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆