简体   繁体   English

服务器发送事件 (SSE) - 自动重新连接

[英]Server sent events (SSE) - automatic reconnect

I am having trouble understanding how the "automatic reconnect" works for SSE.我无法理解 SSE 的“自动重新连接”如何工作。

I've been reading the documentation of Mozilla and WC3 and several other discussions, but still I am still not sure how it works.我一直在阅读 Mozilla 和 WC3 的文档以及其他一些讨论,但我仍然不确定它是如何工作的。

https://html.spec.whatwg.org/multipage/server-sent-events.html https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events https://html.spec.whatwg.org/multipage/server-sent-events.html https-事件/文档/WebAPI/sServer.org

I have implemented SSE using gorilla/mux in Golang and it all works.我已经在 Golang 中使用 gorilla/mux 实现了 SSE,并且一切正常。 Now to the interesting part, I can break the connection from the server but the connection will magically be restored seconds later.现在到了有趣的部分,我可以断开与服务器的连接,但连接会在几秒钟后神奇地恢复。 I haven't implemented any functionality for this myself, so I am not sure how this happens?我自己没有为此实现任何功能,所以我不确定这是怎么发生的?

All major browsers will try to automatically reconnect, however I never manually set any Event ID.所有主流浏览器都会尝试自动重新连接,但我从未手动设置任何事件 ID。 Will the the browser reconnect but just not receive any lost data sent during disconnect?浏览器会重新连接,但不会收到断开连接期间发送的任何丢失数据吗? (this is what the use of EventID can solve, but I have no interest in it). (这就是使用EventID可以解决的问题,但我对它没有兴趣)。 Ie, can the browser reconnect without the use of EventID?即,浏览器是否可以在不使用 EventID 的情况下重新连接? Or could it be something else that makes this "reconnect" possible?或者是其他什么东西使这种“重新连接”成为可能?

As per specification client must send Last-Event-Id header to tell the server about last event id it successfully received.根据规范,客户端必须发送 Last-Event-Id header 来告诉服务器它成功接收到的最后一个事件id Only when events are send with id field with empty value (different from just sending data and ommiting id completely) Last-Event-Id resets on client side (you can think of this as block transactions).仅当使用具有空值的id字段发送事件时(与仅发送数据和完全省略id不同)Last-Event-Id 在客户端重置(您可以将其视为块交易)。

See section 5 Processing Model in https://www.w3.org/TR/2009/WD-eventsource-20090421/参见 https 中的第 5 节处理Model://www.w3.org/TR/2009/WD-eventsource-20090421/

If the event source's last event ID string is not the empty string, then a Last-Event-Id HTTP header must be included with the request, whose value is the value of the event source's last event ID string.如果事件源的最后一个事件 ID 字符串不是空字符串,则 Last-Event-Id HTTP header 必须包含在请求中,其值是事件源的最后一个事件 ID 字符串的值。

There's good explanation of this later in section 7 Interpreting an Event Stream describing on test-stream practical example on how Last-Event-Id sent by client side is affected by value (and presence) of event id .稍后在第 7 节解释事件 Stream 中有很好的解释,描述了客户端发送的 Last-Event-Id 如何受事件id的值(和存在)影响的test-stream实际示例。

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

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