1000
api
protocol_error
ai_generated
true
WebSocket 关闭代码 1000:正常关闭
WebSocket close code 1000: Normal Closure
ID: api/websocket-connection-closed-with-code-1000
75%修复率
80%置信度
1证据数
2023-11-10首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| WebSocket RFC 6455 | active | — | — | — |
| Node.js ws library v8.x | active | — | — | — |
| Python websockets v10.x | active | — | — | — |
根因分析
WebSocket 连接被客户端或服务器正常关闭,通常是由于超时、空闲超时或显式关闭。
English
The WebSocket connection was closed normally by either the client or server, often due to a timeout, idle timeout, or explicit close.
官方文档
https://developer.mozilla.org/en-US/docs/Web/API/CloseEvent解决方案
-
实现带指数退避的自动重连。示例 (JavaScript):function connect() { const ws = new WebSocket('wss://example.com/socket'); ws.onclose = (event) => { if (event.code === 1000) { setTimeout(connect, 1000); } }; } -
发送定期的 WebSocket ping/pong 帧以保持连接活跃,避免空闲超时。示例 (Node.js):ws.ping(); setInterval(() => ws.ping(), 30000);
无效尝试
常见但无效的做法:
-
60% 失败
Reconnecting immediately without investigating the reason for closure may lead to repeated disconnections if the server enforces a rate limit or idle timeout.
-
30% 失败
Disabling WebSocket compression or other features incorrectly may not prevent normal closure due to server-side policies.