diff --git a/client/src/signal.rs b/client/src/signal.rs index 25618bf..dc05137 100644 --- a/client/src/signal.rs +++ b/client/src/signal.rs @@ -10,16 +10,23 @@ use web_sys::{ static SIGNALLING_ADDRESS: &str = "http://192.168.1.3:4546/signal"; thread_local! { -static WEBSOCKET: WebSocket = SignallingChannel::init().unwrap(); + static WEBSOCKET: WebSocket = SignallingChannel::init().unwrap(); -static OFFER_CHANNEL: (Sender, Receiver) = async_channel::unbounded(); -static ANSWER_CHANNEL: (Sender, Receiver) = async_channel::unbounded(); -static ICE_CANDIDATE_CHANNEL: (Sender, Receiver) = async_channel::unbounded(); + static OFFER_CHANNEL: (Sender, Receiver) = async_channel::unbounded(); + static ANSWER_CHANNEL: (Sender, Receiver) = async_channel::unbounded(); + static ICE_CANDIDATE_CHANNEL: (Sender, Receiver) = async_channel::unbounded(); } struct SignallingChannel {} impl SignallingChannel { + fn is_ready() -> bool { + if WEBSOCKET.with(|websocket| websocket.ready_state()) == 1 { + true + } else { + false + } + } fn init() -> Result { let web_socket = WebSocket::new(SIGNALLING_ADDRESS).map_err(|_| Error::WebSocketInitialization)?; @@ -78,7 +85,7 @@ impl SignallingChannel { WEBSOCKET.with(|websocket| { websocket .send_with_str(&json.to_string()) - .map_err(|_| Error::WebSocketSend) + .map_err(|err_val| Error::WebSocketSend(format!("{:?}", err_val))) }) } @@ -106,6 +113,10 @@ impl SignallingChannel { .map_err(|err_val| Error::OfferChannelReceive(err_val.to_string())) } } +pub fn is_communication_ready() -> bool { + SignallingChannel::is_ready() +} + pub fn send_auth(data: &String) -> Result<(), Error> { let offer = Signal::new(&SignalType::Auth, data); diff --git a/client/src/webrtc.rs b/client/src/webrtc.rs index 12db646..5d6944a 100644 --- a/client/src/webrtc.rs +++ b/client/src/webrtc.rs @@ -11,9 +11,12 @@ use web_sys::{ wasm_bindgen::{JsCast, JsValue, prelude::Closure}, }; -use crate::signal::{ - receive_answer, receive_ice_candidate, receive_offer, send_answer, send_ice_candidate, - send_offer, +use crate::{ + signal::{ + is_communication_ready, receive_answer, receive_ice_candidate, receive_offer, send_answer, + send_ice_candidate, send_offer, + }, + sleep, }; pub struct WebRTC { @@ -104,6 +107,10 @@ 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 diff --git a/protocol/src/lib.rs b/protocol/src/lib.rs index c98554c..79d5249 100644 --- a/protocol/src/lib.rs +++ b/protocol/src/lib.rs @@ -25,7 +25,7 @@ pub enum Error { UnexpectedSignalType(SignalType), WebSocketInitialization, WebSocketAuth, - WebSocketSend, + WebSocketSend(String), WebSocketReceivedNothing, WebSocketReceiveQueueReset, WebSocketReceiveQueueLocked, @@ -60,7 +60,7 @@ impl Display for Error { Error::WebSocketInitialization => write!(f, "WebSocket Initialization"), Error::WebSocketAuth => write!(f, "WebSocket Auth"), - Error::WebSocketSend => write!(f, "WebSocket Send"), + Error::WebSocketSend(send_error) => write!(f, "WebSocket Send: {}", send_error), Error::WebSocketReceivedNothing => write!(f, "WebSocket Received Nothing"), Error::WebSocketReceiveQueueReset => write!(f, "WebSocket Receive Queue Reset"), Error::WebSocketReceiveQueueLocked => write!(f, "WebSocket Receive Queue Locked"),