WebSocket Protocol Reference
Reference 为 WebSocket (RFC 6455): framing, handshake, subprotocols, 和 extensions.
Connection Lifecycle
WebSocket connections start with an HTTP Upgrade handshake carrying the Sec-WebSocket-Key header. The server responds with 101 Switching Protocols and a derived accept key. After the handshake, the connection upgrades to the WebSocket protocol with binary framing. The connection remains open until either side sends a Close frame or the TCP connection drops. Ping/Pong frames implement keepalive and latency measurement.
- handshake
- HTTP Upgrade
- framing
- Binary frames after upgrade
- ping/pong
- Keepalive mechanism
101 Switching Protocols opcode + payload measure latency Framing and Extensions
WebSocket frames have opcodes: text (0x1), binary (0x2), close (0x8), ping (0x9), pong (0xA). Payloads can be fragmented across continuation frames. The masking bit requires client-to-server frames to be XOR-masked with a 4-byte key. Extensions like permessage-deflate compress message payloads. Subprotocols negotiated in the handshake (e.g., STOMP, WAMP) define application-level semantics. Maximum frame size is implementation-dependent.
- opcodes
- Text, binary, control
- masking
- Client frames masked
- deflate
- permessage-deflate
0x1-0xA XOR with 4-byte key compression extension