简体   繁体   English

手机上的服务器发送事件 (SSE) 连接丢失

[英]Server-sent events (SSE) connection loss on mobile phones

I'm working on a website where some events are broadcasted to the clients using SSE (EventSource API).我正在一个网站上工作,在该网站上使用 SSE(EventSource API)向客户端广播一些事件。 When testing my site on the mobile version of Chrome for Android, I noticed the connection is lost when the channel is idle for about five minutes.在 Android 版 Chrome 移动版上测试我的网站时,我注意到当频道空闲大约五分钟时连接丢失。

I used several Android devices, with different carriers and different browsers and the result is the same, no matter if the screen is on or off.我用了几台安卓设备,不同的运营商和不同的浏览器,结果都是一样的,不管屏幕是开还是关。 Desktop Chrome seems to keep the connection alive for a longer time.桌面 Chrome 似乎可以让连接保持更长时间。

Couldn't find any info about this, and when trying to debug the issue, all I got was a TCP "FIN" packet received from the telephone IP address about 3 and a half minutes after the last event was sent.找不到任何关于此的信息,在尝试调试问题时,我得到的只是在发送最后一个事件大约 3 分半钟后从电话 IP 地址收到的 TCP“FIN”数据包。

EventSource's onerror event doesn't get fired so I can't know when the connection was dropped to initiate a new one. EventSource 的onerror事件不会被触发,所以我不知道何时断开连接以启动新的连接。

Is there any way to avoid this problem or should I just send some fake message every 30 secs to prevent connection idling?有什么办法可以避免这个问题,还是我应该每 30 秒发送一些假消息来防止连接空闲?

Thanks in advance提前致谢

You can set the retry field to establish the reconnection time of EventSource instance您可以设置retry字段来建立EventSource实例的重连时间

  • If the field name is "retry"如果字段名称是“重试”

    • If the field value consists of only ASCII digits, then interpret the field value as an integer in base ten, and set the event stream's reconnection time to that integer.如果字段值仅包含 ASCII 数字,则将该字段值解释为以 10 为底的整数,并将事件流的重新连接时间设置为该整数。 Otherwise, ignore the field.否则,忽略该字段。

Your connection was probably taken by a "push proxy" - a feature that is designed to improve battery life in phones.您的连接可能被“推送代理”占用 - 一项旨在延长手机电池寿命的功能。

Quote from "https://html.spec.whatwg.org/multipage/server-sent-events.html":引用自“https://html.spec.whatwg.org/multipage/server-sent-events.html”:

User agents running in controlled environments, eg browsers on mobile handsets tied to specific carriers, may offload the management of the connection to a proxy on the network.在受控环境中运行的用户代理,例如绑定到特定运营商的移动手机上的浏览器,可以将连接的管理卸载到网络上的代理。 In such a situation, the user agent for the purposes of conformance is considered to include both the handset software and the network proxy.在这种情况下,出于一致性的目的,用户代理被认为包括手机软件和网络代理。

For example, a browser on a mobile device, after having established a connection, might detect that it is on a supporting network and request that a proxy server on the network take over the management of the connection.例如,移动设备上的浏览器在建立连接后,可能会检测到它在支持网络上,并请求网络上的代理服务器接管连接的管理。 The timeline for such a situation might be as follows:这种情况的时间表可能如下:

  1. Browser connects to a remote HTTP server and requests the resource specified by the author in the EventSource constructor.浏览器连接到远程 HTTP 服务器并请求作者在 EventSource 构造函数中指定的资源。
  2. The server sends occasional messages.服务器偶尔会发送消息。
  3. In between two messages, the browser detects that it is idle except for the network activity involved in keeping the TCP connection alive, and decides to switch to sleep mode to save power.在两条消息之间,浏览器检测到除了保持 TCP 连接活动所涉及的网络活动之外,它处于空闲状态,并决定切换到睡眠模式以节省电量。
  4. The browser disconnects from the server.浏览器与服务器断开连接。
  5. The browser contacts a service on the network, and requests that the service, a "push proxy", maintain the connection instead.浏览器联系网络上的服务,并请求该服务(“推送代理”)改为维持连接。
  6. The "push proxy" service contacts the remote HTTP server and requests the resource specified by the author in the EventSource constructor (possibly including a Last-Event-ID HTTP header, etc). “推送代理”服务联系远程HTTP服务器并请求作者在EventSource构造函数中指定的资源(可能包括Last-Event-ID HTTP头等)。
  7. The browser allows the mobile device to go to sleep.浏览器允许移动设备进入睡眠状态。
  8. The server sends another message.服务器发送另一条消息。
  9. The "push proxy" service uses a technology such as OMA push to convey the event to the mobile device, which wakes only enough to process the event and then returns to sleep. “推送代理”服务使用 OMA 推送等技术将事件传送到移动设备,移动设备唤醒仅足以处理事件,然后返回睡眠状态。

This can reduce the total data usage, and can therefore result in considerable power savings.这可以减少总数据使用量,因此可以节省大量电力。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM