简体   繁体   English

当我们有 http2 双向流时,为什么我们需要 SSE?

[英]Why do we need SSE when we have http2 bidirectional streaming?

This may seem elementary but i didn't find any answers on internet.这可能看起来很简单,但我在互联网上没有找到任何答案。

My understanding of SSE (server sent events) is that data is sent from server to client on a TCP connection when the data is available on the server, if the connection is closed, a new connection is opened by the client.我对SSE (服务器发送事件)的理解是,当服务器上的数据可用时,数据通过TCP连接从服务器发送到客户端,如果连接关闭,客户端会打开一个新连接。

In http2 bidirectional streaming we are streaming the data from server when it is available and connection is kept open.http2双向流中,我们在服务器可用且连接保持打开时从服务器流式传输数据。 This does the same as SSE .这与SSE相同。

I am not referring to http2 server push.我不是指http2服务器推送。

In an HTTP/2 connection, you have individual streams.在 HTTP/2 连接中,您有单独的流。 Those streams (except for the push promises) need to get opened by the client to send a HTTP request, then the server can send an answer.这些流(推送承诺除外)需要由客户端打开以发送 HTTP 请求,然后服务器可以发送答案。

So you still need a stream to be opened by a client, waiting for the server to send some data.所以你仍然需要一个 stream 被客户端打开,等待服务器发送一些数据。

And then the whole connection might break down (just like a HTTP/1.1 connection, or a plain TCP one), which means the client still needs to open a new connection (and open a stream there).然后整个连接可能会中断(就像一个 HTTP/1.1 连接,或者一个普通的 TCP 连接),这意味着客户端仍然需要打开一个新连接(并在那里打开一个 stream)。

So HTTP/2 doesn't solve anything here which would make SSE superfluous.所以 HTTP/2 在这里没有解决任何会使 SSE 变得多余的问题。

As mentioned by Yami Odymel in a comment, the SSE specification also specifies the exact format of the messages being sent.正如 Yami Odymel 在评论中提到的,SSE 规范还指定了发送消息的确切格式。

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

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