NOTE
2.2 WebSocket
What WebSocket is, why it is needed, its role, its relationship with HTTP, and the WebSocket frame format.
This is a historical learning note and may contain outdated or incomplete understanding.
1. What Is WebSocket?
Like HTTP, WebSocket is also a protocol.
2. Why Is WebSocket Needed?
It solves one drawback of HTTP: when the server has data to notify the user about, it cannot actively push it; communication can only be initiated by the client.
3. What Does WebSocket Do?
- Obtain server data in real time.
Before WebSocket, only AJAX polling or long polling could be used. The former requests data from the server at intervals [that is, synchronous non-blocking], while the latter blocks when there is no data until data is returned [that is, synchronous blocking].
- Persistent connections reduce request-header overhead.
With HTTP keep alive, every HTTP Transaction needs to transmit header information, which has high overhead.
4. Relationship Between HTTP and WebSocket

WebSocket borrows HTTP to complete the handshake.
The handshake process is as follows:
-
The client sends a request header.
GET /chat HTTP/1.1 Host: server.example.com Upgrade: websocket//notify the server that the websocket protocol is being initiated Connection: Upgrade//notify the server that the websocket protocol is being initiated Sec-WebSocket-Key: x3JJHMbDL1EzLkh9GBhXDw==//used to verify whether the server supports websocket Sec-WebSocket-Protocol: chat, superchat//which server service needs to be used Sec-WebSocket-Version: 13//version in use Origin: http://example.com -
The server returns a response header.
HTTP/1.1 101 Switching Protocols Upgrade: websocket Connection: Upgrade//upgraded to websocket Sec-WebSocket-Accept: HSmrc0sMlYUkAGmm5OPpG2HaGWk=//corresponds to Sec-WebSocket-Key Sec-WebSocket-Protocol: chat//the service ultimately used
5. WebSocket Frame Format

- FIN
A 1-bit field used to mark whether the current data frame is the last data frame. Because one message may be divided into multiple data frames for transmission, if only one data frame is needed, the first data frame is also the last.
- RSV1, RSV2, RSV3
These three fields each occupy 1 bit. According to the RFC, these three bits are used for extensions. If there is no such requirement, they are set to 0.
- Opcode
As the name suggests, the operation code occupies 4 bits, which is one hexadecimal number. It describes what the transmitted data is or what it is used for. It can only have the following values:
- 0x0 denotes a continuation frame: indicates that the current data frame is a fragmented data frame. When one message needs to be divided into multiple data frames for transmission, opcode needs to be set to 0x0.
- 0x1 denotes a text frame: indicates that the current data frame contains text.
- 0x2 denotes a binary frame: indicates that the current data frame contains binary content; do not convert it to a string.
- 0x8 denotes a connection close: indicates a request to close the connection.
- 0x9 denotes a ping: indicates a Ping request.
- 0xA denotes a pong: indicates a Pong packet. When a Ping request is received, a Pong is automatically returned.
The protocol currently specifies these values. 0x3–0x7 and 0xB–0xF are reserved for other uses.
- MASK
Occupies 1 bit and indicates whether the data uses a mask. The RFC states that data frames sent from the server to the client cannot use a mask, while data frames sent from the client to the server must use a mask.
If the data in a frame uses a mask, the Masking-key section must contain a 32-bit mask used by the server to decode the data.
- Payload len
The length of the data, which uses 7 bits by default.
If the data length is less than 125 bytes (note: bytes), the default 7 bits are used to indicate the data length.
If the data length is 126 bytes, the adjacent 2 bytes are used to store a 16-bit unsigned integer as the data length.
If the data length is greater than 126 bytes, the adjacent 8 bytes are used to store a 64-bit unsigned integer as the data length.
- Masking-key
The data mask. If MASK is set to 0, this section can be omitted. If MASK is set to 1, the Masking-key is a 32-bit mask used to decode the data sent by the client to the server.
- Payload data
This section, also the last section, is the actual data to be sent by the frame and can be of any length.
6. References
- What Is the Principle of WebSocket? Why Can It Implement Persistent Connections? - Zhihu
- WebSocket Tutorial - Ruan Yifeng’s Weblog
- From the Perspective of Persistent Connections, What Is the Difference Between Keep-Alive and WebSocket? - CNode
- WebSocket Protocol (2) - Data Frame Format and Server Data Push | Time Flies
- WebSocket Protocol Frame Parsing - Zhihu
- What Do Long Connections and Short Connections Mean in Network Connections? - Zhihu
Discussion
Sign in with GitHub to comment. Discussions are stored as GitHub Issues.View on GitHub