WebSocket 1001 Going Away vs 1006 Abnormal Closure
Both WebSocket 1001 (Going Away) and 1006 (Abnormal Closure) belong to the WebSocket Close Codes category. 1001 indicates that an endpoint is going away, such as a server shutting down or a browser navigating away from the page. The connection must be closed. Meanwhile, 1006 means that a reserved value indicating the connection was closed abnormally without a Close frame being sent. This code must not be set by an endpoint.
描述
An endpoint is going away, such as a server shutting down or a browser navigating away from the page. The connection must be closed.
何时出现
The server is shutting down for maintenance, or the user navigated away from the page or closed the browser tab while a WebSocket was open.
如何修复
Implement automatic reconnection logic with exponential backoff in your client. If the server is restarting, the client should retry after a short delay.
描述
A reserved value indicating the connection was closed abnormally without a Close frame being sent. This code must not be set by an endpoint.
何时出现
The TCP connection dropped unexpectedly — the server crashed, the network cable was unplugged, or a firewall killed the idle connection. No proper WebSocket Close handshake occurred.
如何修复
Implement reconnection with exponential backoff. Use WebSocket ping/pong frames to detect dead connections early. Check for network-level timeouts or aggressive load balancer idle limits.
主要区别
WebSocket 1001: An endpoint is going away, such as a server shutting down or a browser navigating away from the page. The connection must be closed.
WebSocket 1006: A reserved value indicating the connection was closed abnormally without a Close frame being sent. This code must not be set by an endpoint.
You encounter 1001 when the server is shutting down for maintenance, or the user navigated away from the page or closed the browser tab while a WebSocket was open.
You encounter 1006 when the TCP connection dropped unexpectedly — the server crashed, the network cable was unplugged, or a firewall killed the idle connection. No proper WebSocket Close handshake occurred.
何时使用哪个
For 1001 (Going Away): Implement automatic reconnection logic with exponential backoff in your client. If the server is restarting, the client should retry after a short delay. For 1006 (Abnormal Closure): Implement reconnection with exponential backoff. Use WebSocket ping/pong frames to detect dead connections early. Check for network-level timeouts or aggressive load balancer idle limits.