1000 api protocol_error ai_generated true

WebSocket close code 1000: Normal Closure

ID: api/websocket-connection-closed-with-code-1000

Also available as: JSON · Markdown · 中文
75%Fix Rate
80%Confidence
1Evidence
2023-11-10First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
WebSocket RFC 6455 active
Node.js ws library v8.x active
Python websockets v10.x active

Root Cause

The WebSocket connection was closed normally by either the client or server, often due to a timeout, idle timeout, or explicit close.

generic

中文

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

Official Documentation

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

Workarounds

  1. 75% success Implement automatic reconnection with exponential backoff. Example (JavaScript): function connect() { const ws = new WebSocket('wss://example.com/socket'); ws.onclose = (event) => { if (event.code === 1000) { setTimeout(connect, 1000); } }; }
    Implement automatic reconnection with exponential backoff. Example (JavaScript): function connect() { const ws = new WebSocket('wss://example.com/socket'); ws.onclose = (event) => { if (event.code === 1000) { setTimeout(connect, 1000); } }; }
  2. 80% success Send periodic WebSocket ping/pong frames to keep the connection alive and avoid idle timeout. Example (Node.js): ws.ping(); setInterval(() => ws.ping(), 30000);
    Send periodic WebSocket ping/pong frames to keep the connection alive and avoid idle timeout. Example (Node.js): ws.ping(); setInterval(() => ws.ping(), 30000);

中文步骤

  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);

Dead Ends

Common approaches that don't work:

  1. 60% fail

    Reconnecting immediately without investigating the reason for closure may lead to repeated disconnections if the server enforces a rate limit or idle timeout.

  2. 30% fail

    Disabling WebSocket compression or other features incorrectly may not prevent normal closure due to server-side policies.