From d29126f178afeb49c5c604bae962de75001785a0 Mon Sep 17 00:00:00 2001 From: zqm Date: Fri, 10 Apr 2026 16:08:19 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B0=86=E5=BF=83=E8=B7=B3=E5=8A=9F=E8=83=BD?= =?UTF-8?q?=E6=94=BE=E5=88=B0=E5=BA=93=E4=B8=AD=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Windows/CS/Framework4.0/Updater/Cargo.lock | 1 + Windows/CS/Framework4.0/Updater/src/main.rs | 21 ++++++++++++--------- 2 files changed, 13 insertions(+), 9 deletions(-) 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!("[启动] 开始连接...");