Http connections and talk about long long connection websocket

origin

Http understanding of people know, HTTP protocol has a flaw: communication can only be initiated by the client, for example, we want to know the weather today, the client can make a request to the server, the server returns the query results, HTTP protocol can not features information server actively push this one-way request to the client, the server doomed if there is a continuous state of change, the client is very troublesome to know that we can only use the "poll": from time to time, it issued a challenge to understand the server has no new information, the most typical scenario is a chat room. Inefficient polling, is a waste of resources.
In http1.1, the parameters enabled by default called Keep-alive, the official line is that you can use this as a long connection. So the question is, why has this need a higher websocket it? This websocket and socket what is the difference with the link?

Keep-alive on the shortcomings of

Keep-alive can indeed achieve a long connection, but this is a long connection problem, in essence, is still a client initiates - mode server response, there is no way to do server sends a notification to the client. That is, an HTTP connection may be sent a plurality of Request, receiving a plurality Response. But remember Request = Response, in HTTP always is the case, a request can be only one response. And the response is passive, it can not initiate. Place a picture view of the left is not open Keep-alive, right is turned Keep-alive, can still be seen in a question and answer mode, than just to the left of each omitted closing and opening operations.
Here Insert Picture Description
But websocket is different, websocket is another initiative launched.
Here Insert Picture Description
It can be seen in FIG contrast, conventional HTTP requests with respect to each - response requires client and server establish a connection mode, WebSocket communication mode is similar to a TCP Socket connection length, WebSocket once the connection is established, all subsequent data to in the form of a sequence of transmitted frames. In the client disconnects WebSocket connection or cut off before the end of the Server connection, the client and server do not need to re-initiate the connection request. In the case of massive concurrency and client interaction with the server load large flow of great saves the consumption of network bandwidth resources, there are significant performance advantages, and the client to send and receive messages in the same persistent connection to initiate a real-time obvious advantage.

Explain in detail websocket

WebSocket API is part of the HTML5 standard, but that does not mean WebSocket must use, or can only be used in browser-based applications in HTML.
In WebSocket, only you need to be a server and the browser using the HTTP protocol handshake operation, and then establishes a separate TCP communication channel for data transmission. WebSocket is the same as with the HTTP protocol application layer, but it is a two-way communication protocol is built on top of TCP. websocket process is probably the following steps

  1. Browser, server TCP connection is established, three-way handshake. This is the basis, the transmission control layer of the communication, is not performed if a subsequent failure.
  2. After the TCP connection is successful, the browser version numbers and other information transmitted WebSocket support to the server via HTTP protocol. (HTTP shake hands before the start)
  3. After the server receives the handshake request from a client, the HTTP protocol uses the same feedback data.
  4. After receiving a successful connection message for transmission over TCP communication channel.

That WebSocket in the establishment of the handshake, the data is transmitted via HTTP. But after the establishment of transmission in real time is not required HTTP protocol.
Websocket to see a handshake request and response packets
WebSocket client connection packets

GET /webfin/websocket/ HTTP/1.1
Host: localhost
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Key: xqBt3ImNzJbYqRINxEFlkg==
Origin: 
http://localhost
:8080
Sec-WebSocket-Version: 13

Can be seen, the client initiates the message similar to the traditional WebSocket connection HTTP packets, "Upgrade: websocket" indicates that this parameter is WebSocket type request, "Sec-WebSocket-Key" is a base64 encoding of the encrypted client sends WebSocket Wen, requiring the server must return a corresponding encryption of the "Sec-WebSocket-Accept" response, otherwise the client will throw "error during WebSocket handshake" error and close the connection.

Returns the server receives packet data format similar to:
a WebSocket server response packet

HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: K7DJLdLooIwIG/MOpvWFB3y3FE8=

Value "Sec-WebSocket-Accept" the server using returned to the client after consistent with a client key calculated end, "HTTP / 1.1 101 Switching Protocols" represents the server accepts WebSocket protocol client connections, after such a request - after the response processing, WebSocket client service side of the connection handshake is successful, we can follow the TCP communication.

WebSocket relationship with the Socket

Socket actually not a protocol, but in order to facilitate the use of TCP or UDP abstracted layer, it is a set of interface between the application layer and the transport control layer between.

Socket application layer and the intermediate software TCP / IP protocol suite to communicate abstraction layer, which is a set of interfaces. In design mode, Socket is actually a facade pattern, it is the complexity of TCP / IP protocol suite is hidden behind the Socket interface for users, a simple interface is all set, let Socket to organize data in order to comply with the specified protocol.

When two hosts communicate, must be connected via Socket, Socket TCP connection is established using the TCP / IP protocol. TCP connection is more dependent on the underlying IP protocol, IP protocol connection depends on the level of the lower link layer.

WebSocket is a typical application layer protocol.
Here Insert Picture Description
This article feel good friend, a point like this, then your praise is the driving force of my writing.

Reference: https://www.cnblogs.com/Javi/p/9303020.html

Published 114 original articles · won praise 146 · Views 350,000 +

Guess you like

Origin blog.csdn.net/qq32933432/article/details/97943282