将心跳功能放到库中实现
This commit is contained in:
1
Windows/CS/Framework4.0/Updater/Cargo.lock
generated
1
Windows/CS/Framework4.0/Updater/Cargo.lock
generated
@@ -168,6 +168,7 @@ name = "cube_lib"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"async-trait",
|
||||
"chrono",
|
||||
"futures-util",
|
||||
"http",
|
||||
"serde",
|
||||
|
||||
@@ -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<Box<dyn std::future::Future<Output = ()> + 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!("[启动] 开始连接...");
|
||||
|
||||
Reference in New Issue
Block a user