diff --git a/Windows/CS/Framework4.0/Updater/Cargo.lock b/Windows/CS/Framework4.0/Updater/Cargo.lock index d575b8b..9b03525 100644 --- a/Windows/CS/Framework4.0/Updater/Cargo.lock +++ b/Windows/CS/Framework4.0/Updater/Cargo.lock @@ -168,6 +168,7 @@ name = "cube_lib" version = "0.1.0" dependencies = [ "async-trait", + "chrono", "futures-util", "http", "serde", diff --git a/Windows/CS/Framework4.0/Updater/src/main.rs b/Windows/CS/Framework4.0/Updater/src/main.rs index 21c3596..fa70568 100644 --- a/Windows/CS/Framework4.0/Updater/src/main.rs +++ b/Windows/CS/Framework4.0/Updater/src/main.rs @@ -1435,13 +1435,14 @@ async fn run_updater(debug_mode: bool) -> bool { println!("========================================"); } - // 创建 WebSocket 配置(启用自动重连) + // 创建 WebSocket 配置(启用自动重连和心跳) let config = WebSocketConfig::new(&server_url) .with_client_type("Updater") .with_debug(debug_mode) .with_reconnect(true) // 启用自动重连 .with_reconnect_delay(1000) // 初始延迟 1s - .with_max_reconnect_delay(30000); // 最大延迟 30s + .with_max_reconnect_delay(30000) // 最大延迟 30s + .with_heartbeat(30000); // 30秒心跳间隔 // 创建 WebSocket 客户端 let mut client = WebSocketClient::new(config); @@ -1495,13 +1496,6 @@ async fn run_updater(debug_mode: bool) -> bool { } } - // 回复 pong 心跳响应 - if msg_type == "ping" { - let pong_str = format!(r#"{{"Type":"pong","Data":{{"timestamp":"{}"}}}}"#, ts); - log_print!("{} 发送消息:{}", ts, pong_str); - sender.send(pong_str); - } - // 处理 FileVer 响应(根据当前阶段分发) if msg_type == "FileVer" { if let Some(file_versions) = data.get("Data").and_then(|d| d.get("file_versions")).and_then(|v| v.as_object()) { @@ -2200,6 +2194,15 @@ async fn run_updater(debug_mode: bool) -> bool { }) as Pin + Send + Sync>> }); + // 设置心跳确认回调 + let debug_heartbeat = debug_mode; + client.on_heartbeat_ack(move |latency_ms, server_timestamp| { + if debug_heartbeat { + let ts = chrono::Local::now().format("%Y-%m-%d %H:%M:%S%.3f"); + println!("{} [心跳] pong响应延迟: {}ms, 服务端时间: {}", ts, latency_ms, server_timestamp); + } + }); + // 连接(CubeLib 会自动处理重连) if debug_mode { println!("[启动] 开始连接...");