Fixed pinging

This commit is contained in:
2026-02-18 22:55:58 +01:00
parent 5edbe8c8a1
commit a14a989691
3 changed files with 6 additions and 14 deletions
+2 -1
View File
@@ -25,9 +25,10 @@ async fn main() -> session_rs::Result<()> {
// Send a few messages // Send a few messages
for i in 0..5 { for i in 0..5 {
println!("sending");
let msg = serde_json::json!({ "hello": i }); let msg = serde_json::json!({ "hello": i });
session.send(&msg).await?; session.send(&msg).await?;
tokio::time::sleep(std::time::Duration::from_secs(1000)).await; tokio::time::sleep(std::time::Duration::from_secs(1)).await;
} }
session.close().await?; session.close().await?;
+1 -11
View File
@@ -22,17 +22,7 @@ async fn main() -> session_rs::Result<()> {
} }
}; };
// Simple ping loop session.start_ping_loop();
let ping_session = Arc::clone(&session);
tokio::spawn(async move {
let mut interval = tokio::time::interval(std::time::Duration::from_secs(15));
loop {
interval.tick().await;
if ping_session.send_ping().await.is_err() {
break;
}
}
});
// Read loop // Read loop
loop { loop {
+3 -2
View File
@@ -102,12 +102,13 @@ impl Session {
self.send_frame(0x8, &[]).await self.send_frame(0x8, &[]).await
} }
pub fn start_ping(self: Arc<Self>) { pub fn start_ping_loop(&self) {
let s = self.clone();
tokio::task::spawn(async move { tokio::task::spawn(async move {
let mut interval = tokio::time::interval(std::time::Duration::from_secs(15)); let mut interval = tokio::time::interval(std::time::Duration::from_secs(15));
loop { loop {
interval.tick().await; interval.tick().await;
if self.send_ping().await.is_err() { if s.send_ping().await.is_err() {
break; break;
} }
} }