1002
api
protocol_error
ai_generated
true
WebSocket 关闭代码 1002:协议错误
WebSocket close code 1002: Protocol Error
ID: api/websocket-close-code-1002-protocol-error
80%修复率
85%置信度
1证据数
2023-09-05首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| RFC 6455 | active | — | — | — |
| ws library 8.x | active | — | — | — |
| Socket.IO 4.x | active | — | — | — |
| Spring WebSocket 6.0 | active | — | — | — |
| Node.js ws 8.14+ | active | — | — | — |
根因分析
WebSocket 帧格式错误或包含无效数据,例如保留操作码或分片控制帧。
English
WebSocket frame was malformed or contained invalid data, such as a reserved opcode or fragmented control frame.
官方文档
https://datatracker.ietf.org/doc/html/rfc6455#section-7.4.1解决方案
-
Validate that control frames (e.g., ping, pong, close) are not fragmented. In JavaScript, ensure you are not sending a fragmented ping frame: // Bad: Sending a fragmented ping ws.send(Buffer.from('ping'), { fin: false, opcode: 0x9 }); // Wrong! // Good: Send a complete ping frame ws.ping(); // Uses correct non-fragmented frame -
Check for reserved opcodes (0x3-0x7, 0xB-0xF) in the frame. If using a custom WebSocket library, ensure opcode is in {0x0, 0x1, 0x2, 0x8, 0x9, 0xA}.
无效尝试
常见但无效的做法:
-
90% 失败
The error is immediate and not related to timeouts.
-
80% 失败
The root cause is in the client's frame construction, not server state.