# WebSocket 关闭代码 1000：正常关闭

- **ID:** `api/websocket-connection-closed-with-code-1000`
- **领域:** api
- **类别:** protocol_error
- **错误码:** `1000`
- **验证级别:** ai_generated
- **修复率:** 75%

## 根因

WebSocket 连接被客户端或服务器正常关闭，通常是由于超时、空闲超时或显式关闭。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| WebSocket RFC 6455 | active | — | — |
| Node.js ws library v8.x | active | — | — |
| Python websockets v10.x | active | — | — |

## 解决方案

1. ```
   实现带指数退避的自动重连。示例 (JavaScript)：function connect() { const ws = new WebSocket('wss://example.com/socket'); ws.onclose = (event) => { if (event.code === 1000) { setTimeout(connect, 1000); } }; }
   ```
2. ```
   发送定期的 WebSocket ping/pong 帧以保持连接活跃，避免空闲超时。示例 (Node.js)：ws.ping(); setInterval(() => ws.ping(), 30000);
   ```

## 无效尝试

- **** — Reconnecting immediately without investigating the reason for closure may lead to repeated disconnections if the server enforces a rate limit or idle timeout. (60% 失败率)
- **** — Disabling WebSocket compression or other features incorrectly may not prevent normal closure due to server-side policies. (30% 失败率)
