简体   繁体   English

如何捕获 SSE 事件源确认

[英]How to capture SSE Eventsource acknowledgement

I'm using this code to send an SSE message to the browser client.我正在使用此代码向浏览器客户端发送 SSE 消息。

https://nodejs.org/api/http.html#http_response_write_chunk_encoding_callback https://nodejs.org/api/http.html#http_response_write_chunk_encoding_callback

Node server节点服务器

    response.writeHead(200, {  'Content-Type': 'text/event-stream', 'Access-Control-Allow-Origin': '*' });
    response.write(message);
    response.end();

And for the client I'm using this javascript:对于客户端,我正在使用这个 javascript:

var source = new EventSource('myurl');
source.addEventListener('add', addHandler, false);
source.addEventListener('remove', removeHandler, false);

Everything is working fine, but how the server knows for sure that the client actually received it ?一切正常,但是服务器如何确定客户端确实收到了它? I guess SSE is using TCP, is there any way to received the acknowledgement ?我猜 SSE 正在使用 TCP,有没有办法收到确认?

SSEs are a one to many Push protocol. SSE 是一对多的 Push 协议。 So there is no acknowledgement.所以没有承认。 You could send an AJAX request back on receipt, but there is nothing in the pattern to provide this functionality.您可以在收到后发送 AJAX 请求,但模式中没有提供此功能的任何内容。

SSE is a one way communication protocol to push data from server to client. SSE 是一种将数据从服务器推送到客户端的单向通信协议。

There no way for a client to ack event reception.客户端无法确认事件接收。

If acknowledge is a must have, you probably need a two way communication like websockets.如果确认是必须的,您可能需要像 websockets 这样的双向通信。

I know this is many years old, but none of the answers is correct.我知道这已经很多年了,但没有一个答案是正确的。 1) TCP does indeed ACK the push stream - its standard http! 1)TCP确实确认推送流 - 它的标准http! (though whether your code is at a low-enough level to detect it is a different story) (尽管您的代码是否处于足够低的水平以检测它是另一回事)

2) it's not to difficult to develope your own ACK system (I've done it! - To free up resources when last client disappears) and yes, it tends to go against the "spirit" of the protocol and duplicate to a degree the websocket paradigm...but it is wrong to say its impossible. 2)开发自己的ACK系统并不难(我已经做到了! - 在最后一个客户端消失时释放资源),是的,它往往违背协议的“精神”并在一定程度上复制websocket范式......但说它不可能是错误的。 Send a unique per-client "token" in the first message which the browser saves and starts a js "ping" timer which ajaxes a "still alive" message.在浏览器保存的第一条消息中发送一个唯一的每个客户端“令牌”,并启动一个 js“ping”计时器,它对“仍然活着”消息进行 ajax 处理。 In your erver code, handle the ajax and restart client-stil-alive timer.在您的服务器代码中,处理 ajax 并重新启动 client-stil-alive 计时器。 If that expires, client has gone, clean up / free resources etc.如果过期,客户端已经离开,清理/释放资源等。

Yes its a bit "lumpy" but it works and its not rocket-science difficulty.是的,它有点“笨拙”,但它有效,而且不是火箭科学难度。

just my (very late) 2c worth只是我的(很晚)价值 2c

The attached image was me diagnosing a case where the ACK was missing, but one every other one apart from the indicated one you can see the ACK附加的图像是我诊断 ACK 丢失的情况,但是除了指示的一个之外,您可以每隔一个看到 ACK 在此处输入图片说明

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

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