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

@ -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<Signal>, Receiver<Signal>) = async_channel::unbounded();
static ANSWER_CHANNEL: (Sender<Signal>, Receiver<Signal>) = async_channel::unbounded();
static ICE_CANDIDATE_CHANNEL: (Sender<Signal>, Receiver<Signal>) = async_channel::unbounded();
static OFFER_CHANNEL: (Sender<Signal>, Receiver<Signal>) = async_channel::unbounded();
static ANSWER_CHANNEL: (Sender<Signal>, Receiver<Signal>) = async_channel::unbounded();
static ICE_CANDIDATE_CHANNEL: (Sender<Signal>, Receiver<Signal>) = 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<WebSocket, Error> {
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);

View file

@ -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

View file

@ -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"),