WebSocket

WebSocket 1006 Abnormal Closure vs 1008 Policy Violation

Both WebSocket 1006 (Abnormal Closure) and 1008 (Policy Violation) belong to the WebSocket Close Codes category. 1006 indicates 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. Meanwhile, 1008 means that an endpoint is terminating the connection because it received a message that violates its policy. This is a generic code when none of the other codes (1003, 1009) are suitable.

คำอธิบาย

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.

คำอธิบาย

An endpoint is terminating the connection because it received a message that violates its policy. This is a generic code when none of the other codes (1003, 1009) are suitable.

เมื่อคุณพบเห็น

The server rejected a message because it violated an application-level policy — for example, sending messages too rapidly, exceeding rate limits, or failing authentication after the handshake.

วิธีแก้ไข

Review the server's documented policies and constraints. Check for rate limiting, authentication token expiry, or forbidden message content that triggered the rejection.

ความแตกต่างหลัก

1.

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.

2.

WebSocket 1008: An endpoint is terminating the connection because it received a message that violates its policy. This is a generic code when none of the other codes (1003, 1009) are suitable.

3.

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.

4.

You encounter 1008 when the server rejected a message because it violated an application-level policy — for example, sending messages too rapidly, exceeding rate limits, or failing authentication after the handshake.

ควรใช้อันไหนเมื่อไร

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. For 1008 (Policy Violation): Review the server's documented policies and constraints. Check for rate limiting, authentication token expiry, or forbidden message content that triggered the rejection.

เรียนรู้เพิ่มเติม