简体   繁体   English

关于http / 2的文章中关于双向和全双工的困惑

[英]confusion regarding bidirectional and full-duplex in articles about http/2

Some articles describing http/2 are praising it for being both bidirectional and full-duplex. 一些描述http / 2的文章称赞它是双向的还是全双工的。

AFAIK bidirectional means that communication is in both directions so duplex is by its nature bidirectional, yes? AFAIK双向意味着双向通信,因此双工本质上是双向的,是吗?

Duplex can be created by one simplex stream that is reversed at some specific points (half-duplex) or it can be created as two opposite simplex streams (full-duplex). 双工可以由一个在某些特定点反转的单工流(半双工)创建,也可以作为两个相反的单工流(全双工)创建。

Maybe bidirectional is about how can initiate a message exchange? 也许双向有关如何发起消息交换? In http/1 only client can initiate by sending request to the server in which the server returns a response. 在http / 1中,只有客户端可以通过向服务器发送请求来发起初始化,服务器在该服务器中返回响应。 In http/2 a server can send (push) some resource without being explicitly asked for it. 在http / 2中,服务器可以发送(推送)某些资源,而无需明确要求。 But we can use Server-sent events in http/1.1 (that is server can push messages if it wants to after doing a little bit configuration on both the client and the server, but it's still over http/1.1 protocol). 但是我们可以在http / 1.1中使用Server-sent events (即服务器可以在客户端和服务器上都进行一些配置后,如果需要的话可以推送消息,但是仍然通过http / 1.1协议进行)。

When you think about it you may notice that http/1 is as well bidirectional and full-duplex (since pipelining wouldn't be possible in half-duplex). 考虑一下时,您可能会注意到http / 1既是双向的又是全双工的(因为在半双工中无法进行流水线化)。 So no change here from http/2 perspective. 因此,从http / 2角度看,这里没有任何变化。

What has changed is that http/1 required responses to arrive in exact order in which they were requested. 发生的变化是,http / 1要求响应按请求的确切顺序到达。 http/2 lifts that with streams and multiplexing. http / 2通过流和多路复用提升了这一点。

Bidirectional means you can send data in both directions. 双向意味着您可以双向发送数据。

Full duplex means you can send data in both directions at the same time - you can have two threads, one writing data and one reading data, executing concurrently. 全双工意味着您可以同时在两个方向发送数据-您可以同时执行两个线程,一个写入数据和一个读取数据。

If we take as endpoints "client" and "server" (no matter how many TCP connections between the two), then obviously both HTTP/1.1 and HTTP/2 are full duplex. 如果我们将“客户端”和“服务器”作为端点(无论两者之间有多少TCP连接),那么显然HTTP / 1.1和HTTP / 2都是全双工的。

If we take as endpoints the two ends of a single TCP connection between client and server, again both HTTP/1.1 and HTTP/2 are - in general - full duplex. 如果我们将客户端和服务器之间的单个TCP连接的两端作为端点,则HTTP / 1.1和HTTP / 2都是(通常)全双工。

This is obvious for HTTP/2 but less known for HTTP/1.1 because it is commonly thought as a "first the request, then the response" protocol - however, that is not the case. 这对于HTTP / 2是显而易见的,但对于HTTP / 1.1却鲜为人知,因为通常认为它是“先请求,然后响应”协议-但是,情况并非如此。 It is perfectly possible, for example for a server that echoes back the content bytes that the client sends, to have the client make a large upload, and while the upload is still going the server already starts to respond echoing back the bytes - upload and download happen at the same time. 例如,对于服务器来说,完全有可能回显客户端发送的内容字节,让客户端进行较大的上传,并且在上传继续进行时,服务器已经开始响应回显这些字节-上载和下载同时发生。

We can now enter the matter of unsolicited communication from server to client. 现在,我们可以输入服务器与客户端之间未经请求的通信问题。

This is not possible in HTTP/1.1. 在HTTP / 1.1中这是不可能的。 Even with Server Sent Events (SSE) the client makes a request and the server responds with an "infinite response" - but the client must make the request first. 即使使用服务器发送事件(SSE),客户端也会发出请求,而服务器也会以“无限响应”进行响应-但是客户端必须首先发出请求。

In HTTP/1.1, SSE is not full duplex from the point of view of a single TCP connection: the client first makes the request, then the server responds with an "infinite response". 在HTTP / 1.1中,从单个TCP连接的角度来看,SSE不是全双工的:客户端首先发出请求,然后服务器以“无限响应”进行响应。 From that point on, the client can communicate with the server only by making another request, which means opening a new connection. 从那时起,客户端只能通过发出另一个请求来与服务器通信,这意味着打开一个新连接。

In HTTP/2, SSE is full duplex because it would be possible for the client to communicate to the server by making another request on the same TCP connection, thanks to HTTP/2 multiplexing. 在HTTP / 2中,SSE是全双工的,这是由于HTTP / 2多路复用,使得客户端可以通过在同一TCP连接上发出另一个请求来与服务器通信。

The SSE "infinite response" can be seen as "the server writes chunks of data that can be interpreted as pushed messages" but the SSE protocol is too simple to allow generic messages from server to client (eg data cannot be binary). SSE的“无限响应”可以看作是“服务器写入了可以解释为推送消息的数据块”,但是SSE协议太简单了,无法允许从服务器到客户端的通用消息(例如,数据不能是二进制的)。 You would not consider a download that stutters as the server pushing data to the client :) 当服务器将数据推送到客户端时,您不会认为下载过程会中断:)

Unsolicited communication from server to client is also not possible in HTTP/2, because HTTP/2 can "push" a resource to the client, but only in the context of a previous request. 在HTTP / 2中也无法进行从服务器到客户端的未经请求的通信,因为HTTP / 2可以将资源“推”到客户端,但只能在先前请求的上下文中。

For example, a HTTP/2 client establishes a connection with the server, but then it does not send any request; 例如,一个HTTP / 2客户端与服务器建立了连接,但是它没有发送任何请求。 in this case, the server won't be able to push anything to the client (not even a welcome page), as it needs a previous request to do so. 在这种情况下,服务器将无法向客户端推送任何内容(甚至没有欢迎页面),因为它需要先前的请求。

That is why HTTP/2 cannot be a complete substitute for the WebSocket protocol, which is the only web protocol that you can use for full unsolicited communication from server to client. 这就是为什么HTTP / 2不能完全替代WebSocket协议的原因,WebSocket协议是唯一可用于从服务器到客户端的完全未经请求的通信的Web协议。

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

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