# WebSocket 关闭代码 1001：端点不可用

- **ID:** `api/websocket-close-code-1001-endpoint-unavailable`
- **领域:** api
- **类别:** network_error
- **错误码:** `1001`
- **验证级别:** ai_generated
- **修复率:** 75%

## 根因

服务器正在关闭或 WebSocket 端点暂时不可用，导致使用代码 1001 有意关闭连接。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| WebSocket Protocol RFC 6455 | active | — | — |
| Node.js ws library 8.x | active | — | — |
| Python websockets 11.x | active | — | — |
| Spring WebSocket 6.0+ | active | — | — |

## 解决方案

1. ```
   Implement exponential backoff reconnection logic. Example in JavaScript:
let delay = 1000;
function reconnect() {
  setTimeout(() => {
    const ws = new WebSocket('wss://api.example.com/socket');
    ws.onclose = (event) => {
      if (event.code === 1001) {
        delay = Math.min(delay * 2, 30000);
        reconnect();
      }
    };
  }, delay);
}
   ```
2. ```
   Check server health endpoint before reconnecting to ensure the WebSocket server is back online.
   ```
3. ```
   If using a load balancer, verify that health checks are configured correctly for the WebSocket service to avoid routing to unhealthy instances.
   ```

## 无效尝试

- **** — The server may still be unavailable; immediate reconnection often fails repeatedly and increases server load. (80% 失败率)
- **** — Code 1001 is a server-initiated close due to unavailability, not a protocol version mismatch. (90% 失败率)
- **** — Compression settings are unrelated to server endpoint availability. (95% 失败率)
