# WebSocket收到状态码为1006的关闭帧（异常关闭）

- **ID:** `communication/websocket-1006-abnormal-closure`
- **领域:** communication
- **类别:** connection_error
- **错误码:** `1006`
- **验证级别:** ai_generated
- **修复率:** 70%

## 根因

WebSocket连接在没有关闭帧的情况下意外终止，通常由网络故障、代理超时或服务器崩溃引起。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| RFC 6455 | active | — | — |
| WebSocket++ 0.8.2 | active | — | — |
| ws 8.16 | active | — | — |
| Node.js 20 | active | — | — |

## 解决方案

1. ```
   Implement automatic reconnection with exponential backoff on the client. When a 1006 closure is detected, wait 1s, 2s, 4s, etc., before reconnecting. Log the event to diagnose network stability.
   ```
2. ```
   Check proxy and load balancer settings for WebSocket timeout. On NGINX, ensure proxy_read_timeout is set high (e.g., 3600s) and proxy_http_version is 1.1 with Connection upgrade.
   ```

## 无效尝试

- **Increase the WebSocket ping/pong interval on the client** — 1006 is not a timeout error; it's an abnormal closure where no close frame is sent. Ping/pong intervals only help with idle timeouts, not sudden disconnections (70% 失败率)
- **Disable SSL/TLS on the WebSocket connection** — 1006 can occur over both ws:// and wss://; disabling encryption does not prevent network-level drops (85% 失败率)
- **Switch from WebSocket to long polling as a workaround** — Long polling introduces different failure modes and does not fix the root cause of unstable WebSocket connections (90% 失败率)
