From 1e5618bfb39f0b0f415a89e9af6c5d0d59cdaf37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C5=91rik=20Levente?= <33373714+Levminer@users.noreply.github.com> Date: Fri, 19 Jul 2024 23:54:38 +0200 Subject: [PATCH] Daemon fix RTC connections --- platforms/unix/daemon/src/main.rs | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/platforms/unix/daemon/src/main.rs b/platforms/unix/daemon/src/main.rs index 57205dd..2785320 100644 --- a/platforms/unix/daemon/src/main.rs +++ b/platforms/unix/daemon/src/main.rs @@ -23,6 +23,7 @@ use tower_http::{ cors::{Any, CorsLayer}, trace::{DefaultMakeSpan, TraceLayer}, }; +use webrtc::data_channel::data_channel_state::RTCDataChannelState; use webrtc::data_channel::RTCDataChannel; use webrtc::ice_transport::ice_server::RTCIceServer; @@ -132,16 +133,19 @@ async fn main() { tokio::spawn(async move { loop { - let hw_message = receiver.recv().await.unwrap(); - let network_data = NetworkData { - r#type: "data".to_string(), - data: hw_message.clone(), - }; - - dc.send_text(serde_json::to_string(&network_data).unwrap()) - .await - .unwrap(); - tokio::time::sleep(std::time::Duration::from_secs(3)).await; + if dc.ready_state() == RTCDataChannelState::Open { + let hw_message = receiver.recv().await.unwrap(); + let network_data = NetworkData { + r#type: "data".to_string(), + data: hw_message.clone(), + }; + + dc.send_text(serde_json::to_string(&network_data).unwrap()) + .await + .unwrap(); + } + + tokio::time::sleep(std::time::Duration::from_secs(2)).await; } }); } @@ -166,7 +170,7 @@ async fn main() { info!("RTC started"); loop { - tokio::time::sleep(std::time::Duration::from_secs(5)).await; + tokio::time::sleep(std::time::Duration::from_secs(100)).await; } });