# WebSocket 关闭码 1007：负载数据与端点协议类型不一致

- **ID:** `api/websocket-close-1007-payload-data`
- **领域:** api
- **类别:** protocol_error
- **错误码:** `1007`
- **验证级别:** ai_generated
- **修复率:** 85%

## 根因

服务器在仅文本的 WebSocket 端点上收到了二进制帧，或文本帧包含无效的 UTF-8 编码。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| websockets 10.0 | active | — | — |
| ws 8.13.0 | active | — | — |
| Spring WebSocket 6.0 | active | — | — |
| nodejs 20.x | active | — | — |

## 解决方案

1. ```
   确保客户端发送文本帧（操作码 0x1）而不是二进制帧（操作码 0x2）。在 JavaScript WebSocket API 中，始终使用 `ws.send('字符串数据')`，不要使用 `ws.send(new ArrayBuffer(...))`。
   ```
2. ```
   如果确实需要二进制数据，请将服务器端点配置为接受二进制帧（例如，在 Spring WebSocket 中使用 `extends BinaryWebSocketHandler` 而不是 `TextWebSocketHandler`）。
   ```

## 无效尝试

- **** — The issue is protocol-level, not a transient connection glitch. Restarting only resets state temporarily without fixing the frame type mismatch. (90% 失败率)
- **** — Buffer size doesn't affect frame type validation; the server still rejects binary frames on text endpoints regardless of buffer limits. (95% 失败率)
