概述
WebSocket 连接管理工作流是一个中级难度的工作流。实现 WebSocket 连接生命周期管理,包含重连、心跳和优雅关闭。该工作流的目标是:Build a robust WebSocket client that handles disconnections and reconnections transparently.
环境准备
执行本工作流需要以下环境:A WebSocket server endpoint;A client application needing real-time updates;A reconnection strategy with exponential backoff。请确保在开始前准备好所有必需的工具和资源,以避免中途因环境问题中断。
执行步骤
本工作流包含4个步骤。步骤1:Implement heartbeat——Add a ping-pong heartbeat to detect dead connections within 30 seconds. 步骤2:Add reconnection——Implement automatic reconnection with exponential backoff after disconnection. 步骤3:Handle messages——Add message queuing during disconnection and flush on reconnect. 步骤4:Graceful shutdown——Implement clean connection closure on page unload or app shutdown.
验证与清理
预期结果:WebSocket connections are managed with heartbeat, reconnection, and graceful shutdown for reliable communication.。验证方法:Disconnect the network and verify the client reconnects; close the app and confirm no orphaned connections on the server.。清理操作:Clean up WebSocket connections on page unload to avoid server-side resource leaks.。estimatedMinutes 为 50 分钟。
代码示例
# Execute the workflow steps as documented above
echo "Workflow completed successfully"