From 16481ed7bd41c8a4d090cef3c6b17059865bb7a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Mon, 28 Apr 2025 05:18:36 +0300 Subject: [PATCH] fix: :zap: now whole gui waits for websocket connection establishment --- client/src/gui.rs | 8 ++++++-- client/src/signal.rs | 9 +++++++-- client/src/webrtc.rs | 13 +++---------- 3 files changed, 16 insertions(+), 14 deletions(-) diff --git a/client/src/gui.rs b/client/src/gui.rs index 2b98af3..f5ec76f 100644 --- a/client/src/gui.rs +++ b/client/src/gui.rs @@ -7,13 +7,17 @@ use leptos::{ }; use wasm_bindgen_futures::spawn_local; -use crate::{media::audio, webrtc::WebRTC}; +use crate::{media::audio, signal::wait_until_communication_is_ready, webrtc::WebRTC}; pub fn app() -> impl IntoView { let audio_stream = LocalResource::new(|| audio()); + let wait_until_communication_is_ready = + LocalResource::new(|| wait_until_communication_is_ready()); let props = ShowProps::builder() - .when(move || audio_stream.read().is_some()) + .when(move || { + audio_stream.read().is_some() && wait_until_communication_is_ready.read().is_some() + }) .children(ToChildren::to_children(move || { let audio_stream = audio_stream.read(); let audio_stream = audio_stream.as_deref().unwrap().clone(); diff --git a/client/src/signal.rs b/client/src/signal.rs index dc05137..c7eb400 100644 --- a/client/src/signal.rs +++ b/client/src/signal.rs @@ -7,6 +7,8 @@ use web_sys::{ wasm_bindgen::{JsCast, prelude::Closure}, }; +use crate::sleep; + static SIGNALLING_ADDRESS: &str = "http://192.168.1.3:4546/signal"; thread_local! { @@ -113,8 +115,11 @@ impl SignallingChannel { .map_err(|err_val| Error::OfferChannelReceive(err_val.to_string())) } } -pub fn is_communication_ready() -> bool { - SignallingChannel::is_ready() + +pub async fn wait_until_communication_is_ready() { + while !SignallingChannel::is_ready() { + sleep(100).await; + } } pub fn send_auth(data: &String) -> Result<(), Error> { diff --git a/client/src/webrtc.rs b/client/src/webrtc.rs index 5d6944a..12db646 100644 --- a/client/src/webrtc.rs +++ b/client/src/webrtc.rs @@ -11,12 +11,9 @@ use web_sys::{ wasm_bindgen::{JsCast, JsValue, prelude::Closure}, }; -use crate::{ - signal::{ - is_communication_ready, receive_answer, receive_ice_candidate, receive_offer, send_answer, - send_ice_candidate, send_offer, - }, - sleep, +use crate::signal::{ + receive_answer, receive_ice_candidate, receive_offer, send_answer, send_ice_candidate, + send_offer, }; pub struct WebRTC { @@ -107,10 +104,6 @@ impl WebRTC { } pub async fn offer(&self) -> Result<(), Error> { - while !is_communication_ready() { - log!("Waiting for Communication State"); - sleep(100).await; - } let offer_promise = self.peer_connection.create_offer(); match JsFuture::from(offer_promise) .await