fix: webrtc offer now waits for initial readiness of websocket

This commit is contained in:
Ahmet Kaan Gümüş 2025-04-28 02:42:26 +03:00
parent dd0c0cf42a
commit 693c6353a9
3 changed files with 28 additions and 10 deletions

View file

@ -20,6 +20,13 @@ static ICE_CANDIDATE_CHANNEL: (Sender<Signal>, Receiver<Signal>) = async_channel
struct SignallingChannel {} struct SignallingChannel {}
impl SignallingChannel { impl SignallingChannel {
fn is_ready() -> bool {
if WEBSOCKET.with(|websocket| websocket.ready_state()) == 1 {
true
} else {
false
}
}
fn init() -> Result<WebSocket, Error> { fn init() -> Result<WebSocket, Error> {
let web_socket = let web_socket =
WebSocket::new(SIGNALLING_ADDRESS).map_err(|_| Error::WebSocketInitialization)?; WebSocket::new(SIGNALLING_ADDRESS).map_err(|_| Error::WebSocketInitialization)?;
@ -78,7 +85,7 @@ impl SignallingChannel {
WEBSOCKET.with(|websocket| { WEBSOCKET.with(|websocket| {
websocket websocket
.send_with_str(&json.to_string()) .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())) .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> { pub fn send_auth(data: &String) -> Result<(), Error> {
let offer = Signal::new(&SignalType::Auth, data); let offer = Signal::new(&SignalType::Auth, data);

View file

@ -11,9 +11,12 @@ use web_sys::{
wasm_bindgen::{JsCast, JsValue, prelude::Closure}, wasm_bindgen::{JsCast, JsValue, prelude::Closure},
}; };
use crate::signal::{ use crate::{
receive_answer, receive_ice_candidate, receive_offer, send_answer, send_ice_candidate, signal::{
send_offer, is_communication_ready, receive_answer, receive_ice_candidate, receive_offer, send_answer,
send_ice_candidate, send_offer,
},
sleep,
}; };
pub struct WebRTC { pub struct WebRTC {
@ -104,6 +107,10 @@ impl WebRTC {
} }
pub async fn offer(&self) -> Result<(), Error> { 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(); let offer_promise = self.peer_connection.create_offer();
match JsFuture::from(offer_promise) match JsFuture::from(offer_promise)
.await .await

View file

@ -25,7 +25,7 @@ pub enum Error {
UnexpectedSignalType(SignalType), UnexpectedSignalType(SignalType),
WebSocketInitialization, WebSocketInitialization,
WebSocketAuth, WebSocketAuth,
WebSocketSend, WebSocketSend(String),
WebSocketReceivedNothing, WebSocketReceivedNothing,
WebSocketReceiveQueueReset, WebSocketReceiveQueueReset,
WebSocketReceiveQueueLocked, WebSocketReceiveQueueLocked,
@ -60,7 +60,7 @@ impl Display for Error {
Error::WebSocketInitialization => write!(f, "WebSocket Initialization"), Error::WebSocketInitialization => write!(f, "WebSocket Initialization"),
Error::WebSocketAuth => write!(f, "WebSocket Auth"), 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::WebSocketReceivedNothing => write!(f, "WebSocket Received Nothing"),
Error::WebSocketReceiveQueueReset => write!(f, "WebSocket Receive Queue Reset"), Error::WebSocketReceiveQueueReset => write!(f, "WebSocket Receive Queue Reset"),
Error::WebSocketReceiveQueueLocked => write!(f, "WebSocket Receive Queue Locked"), Error::WebSocketReceiveQueueLocked => write!(f, "WebSocket Receive Queue Locked"),