WebSocket Practice Part 2 Protocol Analysis

I. Introduction

The previous article Part 1   of WebSocket Practice  talked about a minimalist example and basic API introduction of WebSocket. This article will analyze the WebSocket protocol. The best way to learn network protocols is to capture packets and analyze everything to understand everything.

2. WebSocket protocol

I originally wanted to steal a picture from the Internet, but then I thought it was not a good idea, so I drew one myself.

1. WebSocket handshake

The WebSocket handshake is completed using a special HTTP request and response. The client handshake request header will carry Upgrade: websocket, indicating a request to upgrade the protocol to the WebSocket protocol.

Response code 101 indicates agreement to upgrade the protocol. Sec-WebSocket-Accept is inherited from the requested Sec-WebSocket-key and contains a special response value that must exactly match the client's expectations for the handshake to succeed. Sec-WebSocket-Accept=Base64(SHA1(Sec-WebSocket-key+KEY_SUFFIX)).

2. Send data

After the WebSocket handshake is successfully opened and the connection is opened, the client and server can send data to each other at any time. In yesterday's simple example, the client's open event was added to send information, and the sent messages and received messages can be seen in the browser. This is the most essential difference from HTTP. As long as the handshake is completed, no matter which end wants to send data, it will be sent. Instead of like HTTP, the server must wait for the client's request and then respond.

Change the client example in the previous section to send one piece of data every 5 seconds.

In the Message tab of the browser console, you can view the data of Send (up arrow) and Receive (down arrow), which are sent to each other in full duplex.

3. Turn off handshake

The endpoint that terminates the connection sends a numeric code along with a closing reason string. The closing handshake can gracefully close the connection, allowing applications to distinguish whether the connection was closed intentionally or accidentally.

3. Packet capture analysis

IP:115.192.133.59 WebSocket client

IP:172.16.79.224 WebSocket server (intranet IP)

In order to make it easier to view the content of the data packet, the code is slightly adjusted to remove the continuous data sent by the client and server.

The server changes to sending three pieces of data in succession.

The client instead does not send the message but only closes the websocket after receiving the message

1. [1, 2, 3] The first three packets are ordinary TCP three-way handshake.

2. [4] The fourth packet is an ordinary HTTP request that carries Upgrade: websocket in the request header.

3. [5] A TCP response packet from the server, [6] An HTTP response packet from the server, and 101 Switching Protocols tells the client protocol to upgrade.

4. [7, 8, 9] In the 7th, 8th, and 9th packages, the server sends three packages to the client using the WebSocket protocol. WebSocket sends the message content in frame format.

  • Fin : 1 bit, used to indicate that this is the last message fragment of a message. Of course, the first message fragment may also be the last message fragment.

  • RSV1, RSV2, RSV3 : Each is 1 bit. If there is no custom protocol agreed between the two parties, the value of these bits must be 0, otherwise the WebSocket connection must be disconnected.

  • Opcode : 4-bit opcode, which defines the payload data. If an unknown opcode is received, the connection must be disconnected. The following are the defined opcodes.

  • Payload length : The length of the transmitted data, expressed in bytes.

  • Payload data : Payload data, the data transmitted in this frame is [b].

5. [10, 11, 12, 13] The client sends 4 TCP response packets to the server, which should be a response to the received message.

6. [14] The client calls the close method to close the websocket connection. You can see that the server package has been sent out before closing.

7. [15] The server responded to the client's close call and closed the connection.

8. [16, 17, 18, 19, 20] TCP waves 4 times, and I don’t know what the other packet does.

4. Summary

WebSocket is based on TCP. It is also an application layer protocol like HTTP. However, the first handshake of WebSocket relies on the HTTP protocol. If the handshake is successful, the protocol is upgraded to WebSocket. Then the long connection can send messages to each other continuously instead of HTTP requests. /Response mechanism. In addition, you can see that after the client's port randomly uses a value of 52914, it will always use the same port before it is closed, so that it can be found. The IP is used to find the machine, and the port is used to find the application.

Note: The next article will introduce the heartbeat mechanism and disconnection and reconnection mechanism.

Guess you like

Origin blog.csdn.net/2301_76787421/article/details/133501544