Regularly send pings
Signed-off-by: Marcel Müller <neikos@neikos.email>
This commit is contained in:
parent
82a8418b82
commit
0af3def21f
1 changed files with 20 additions and 5 deletions
|
|
@ -12,7 +12,7 @@ use clap::Parser;
|
|||
use dashmap::DashMap;
|
||||
use futures::{SinkExt, StreamExt};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::{net::SocketAddr, path::PathBuf, sync::Arc};
|
||||
use std::{net::SocketAddr, path::PathBuf, sync::Arc, time::Duration};
|
||||
use tokio::sync::broadcast;
|
||||
use tower_http::cors::CorsLayer;
|
||||
use tracing::{info, warn};
|
||||
|
|
@ -386,12 +386,27 @@ async fn handle_socket(socket: WebSocket, state: Arc<AppState>, peer_ip: String)
|
|||
|
||||
// Task to forward broadcast messages to this peer's websocket
|
||||
let mut send_task = tokio::spawn(async move {
|
||||
while let Ok(msg) = rx.recv().await {
|
||||
loop {
|
||||
let timer = tokio::time::sleep(Duration::from_secs(1));
|
||||
tokio::select! {
|
||||
msg = rx.recv() => {
|
||||
if let Ok(msg) = msg {
|
||||
let text = serde_json::to_string(&msg).unwrap();
|
||||
if sender.send(Message::Text(text.into())).await.is_err() {
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
break
|
||||
}
|
||||
}
|
||||
_ = timer => {
|
||||
if sender.send(Message::Ping(Default::default())).await.is_err() {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
// Task to handle incoming messages from this peer
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue