RFC 6455 - The WebSocket Protocol (WebSocket协议)
发布日期: 2011年12月
状态: 标准跟踪协议 (Standards Track)
作者: I. Fette (Google Inc.), A. Melnikov (Isode Ltd.)
摘要 (Abstract)
WebSocket协议在单个TCP连接上实现客户端和服务器之间的全双工通信。WebSocket协议旨在被在Web浏览器和Web服务器中实现,但它可以被任何客户端或服务器应用程序使用。
WebSocket协议是一个独立的基于TCP的协议。它与HTTP的唯一关系是其握手被HTTP服务器解释为升级请求。
目录 (Table of Contents)
- 1. Introduction (简介)
- 1.1 Background
- 1.2 Protocol Overview
- 1.3 Opening Handshake
- 1.4 Closing Handshake
- 1.5 Design Philosophy
- 1.6 Security Model
- 1.7 Relationship to TCP and HTTP
- 1.8 Establishing a Connection
- 1.9 Subprotocols and Extensions
- 2. Conformance Requirements (符合性要求)
- 2.1 Terminology and Other Conventions
- 3. WebSocket URIs
- 4. Opening Handshake (开放握手)
- 4.1 Client Requirements
- 4.2 Server-Side Requirements
- 4.3 Collecting Extensions and Subprotocols
- 4.4 Supporting Multiple Versions
- 5. Data Framing (数据帧)
- 5.1 Overview
- 5.2 Base Framing Protocol
- 5.3 Client-to-Server Masking
- 5.4 Fragmentation
- 5.5 Control Frames
- 5.6 Data Frames
- 5.7 Examples
- 5.8 Extensibility
- 6. Sending and Receiving Data (发送和接收数据)
- 6.1 Sending Data
- 6.2 Receiving Data
- 7. Closing the Connection (关闭连接)
- 7.1 Definitions
- 7.2 Abnormal Closures
- 7.3 Normal Closure
- 7.4 Status Codes
- 8. Error Handling (错误处理)
- 8.1 Handling Errors in UTF-8-Encoded Data
- 9. Extensions (扩展)
- 9.1 Negotiating Extensions
- 9.2 Known Extensions
- 10. Security Considerations (安全考虑)
- 10.1 Non-Browser Clients
- 10.2 Origin Considerations
- 10.3 Attacks On Infrastructure
- 10.4 Implementation-Specific Limits
- 10.5 WebSocket Client Authentication
- 10.6 Connection Confidentiality and Integrity
- 10.7 Handling of Invalid Data
- 10.8 Use of SHA-1
- 11. IANA Considerations
- 12. Using WebSocket Protocol from Other Specifications
- 13. Acknowledgements (致谢)
- 14. References (参考文献)
- 14.1 Normative References
- 14.2 Informative References
附加资源
- 📖 实现指南: WebSocket协议实现指南 - 详细的技术说明、代码示例和最佳实践
- 🔗 官方原文: RFC 6455
- 📋 官方页面: RFC 6455 DataTracker
- 🐛 勘误表: RFC Editor Errata
相关RFC
- RFC 6455: The WebSocket Protocol ← 本文档
- RFC 7692: WebSocket Compression Extensions (WebSocket压缩扩展)
- RFC 8441: Bootstrapping WebSockets with HTTP/2 (在HTTP/2上引导WebSocket)
- RFC 6455: WebSocket Protocol Security Considerations
快速参考
WebSocket URI格式
ws-URI = "ws:" "//" host [ ":" port ] path [ "?" query ]
wss-URI = "wss:" "//" host [ ":" port ] path [ "?" query ]
ws://- 非加密连接 (默认端口80)wss://- TLS加密连接 (默认端口443,生产环境推荐)
握手示例
客户端请求:
GET /chat HTTP/1.1
Host: server.example.com
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==
Sec-WebSocket-Version: 13
Origin: http://example.com
服务器响应:
HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=
帧类型 (Opcode)
| Opcode | 类型 | 说明 |
|---|---|---|
| 0x0 | Continuation | 延续帧 |
| 0x1 | Text | 文本帧 (UTF-8) |
| 0x2 | Binary | 二进制帧 |
| 0x8 | Close | 关闭帧 |
| 0x9 | Ping | Ping帧 (心跳检测) |
| 0xA | Pong | Pong帧 (心跳响应) |
关闭状态码
| 代码 | 名称 | 说明 |
|---|---|---|
| 1000 | Normal Closure | 正常关闭 |
| 1001 | Going Away | 端点离开 (如页面导航) |
| 1002 | Protocol Error | 协议错误 |
| 1003 | Unsupported Data | 不支持的数据类型 |
| 1006 | Abnormal Closure | 异常关闭 (未发送Close帧) |
| 1009 | Message Too Big | 消息过大 |
| 1011 | Internal Error | 服务器内部错误 |
帧结构 (基本格式)
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-------+-+-------------+-------------------------------+
|F|R|R|R| opcode|M| Payload len | Extended payload length |
|I|S|S|S| (4) |A| (7) | (16/64) |
|N|V|V|V| |S| | (if payload len==126/127) |
| |1|2|3| |K| | |
+-+-+-+-+-------+-+-------------+-------------------------------+
| Extended payload length continued, if payload len == 127 |
+-------------------------------+-------------------------------+
| |Masking-key, if MASK set to 1 |
+-------------------------------+-------------------------------+
| Masking-key (continued) | Payload Data |
+-------------------------------+-------------------------------+
关键点:
- FIN: 1=最后一帧, 0=还有后续帧
- MASK: 客户端→服务器必须为1, 服务器→客户端必须为0
- Opcode: 帧类型标识
- Payload Length: 数据长度 (0-125直接表示, 126用16位, 127用64位)
核心术语表
| 英文术语 | 中文译法 | 说明 |
|---|---|---|
| Full-Duplex Communication | 全双工通信 | 双向同时通信 |
| Opening Handshake | 开放握手 | HTTP升级到WebSocket的过程 |
| Closing Handshake | 关闭握手 | 优雅关闭连接的过程 |
| Frame | 帧 | 数据传输的基本单位 |
| Message | 消息 | 由一个或多个帧组成 |
| Masking | 掩码 | 客户端数据必须掩码(安全机制) |
| Fragmentation | 分片 | 大消息分多帧传输 |
注意: 本RFC的各章节翻译正在进行中。部分章节可能暂未提供,请参考实现指南获取完整的技术说明、代码示例和最佳实践。