1001 api network_error ai_generated partial

WebSocket 关闭代码 1001:端点不可用

WebSocket close code 1001: Endpoint Unavailable

ID: api/websocket-close-code-1001-endpoint-unavailable

其他格式: JSON · Markdown 中文 · English
75%修复率
82%置信度
1证据数
2023-06-20首次发现

版本兼容性

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

根因分析

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

English

The server is shutting down or the WebSocket endpoint is temporarily unavailable, causing an intentional close with code 1001.

generic

官方文档

https://developer.mozilla.org/en-US/docs/Web/API/CloseEvent/statusCode

解决方案

  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.

无效尝试

常见但无效的做法:

  1. 80% 失败

    The server may still be unavailable; immediate reconnection often fails repeatedly and increases server load.

  2. 90% 失败

    Code 1001 is a server-initiated close due to unavailability, not a protocol version mismatch.

  3. 95% 失败

    Compression settings are unrelated to server endpoint availability.