diff options
author | 2025-04-29 20:02:08 +0100 | |
---|---|---|
committer | 2025-04-29 20:02:08 +0100 | |
commit | 1f1f267df88638229f09b875e72fd118c08a7ae7 (patch) | |
tree | 0f6a05ae037a1855dea9a275aa6400d474be1d36 | |
parent | 42c7423667a2d6acdebca75250ad30c5d475081b (diff) | |
download | luz-1f1f267df88638229f09b875e72fd118c08a7ae7.tar.gz luz-1f1f267df88638229f09b875e72fd118c08a7ae7.tar.bz2 luz-1f1f267df88638229f09b875e72fd118c08a7ae7.zip |
feat(luz): proper websocket connection errors
-rw-r--r-- | luz/Cargo.toml | 1 | ||||
-rw-r--r-- | luz/src/connection/ws.rs | 39 | ||||
-rw-r--r-- | luz/src/error.rs | 12 |
3 files changed, 46 insertions, 6 deletions
diff --git a/luz/Cargo.toml b/luz/Cargo.toml index 6dee784..0543768 100644 --- a/luz/Cargo.toml +++ b/luz/Cargo.toml @@ -40,6 +40,7 @@ uuid = { version = "1.13.1", features = ["js", "v4"] } getrandom = { version = "0.2.15", features = ["js"] } stanza = { version = "0.1.0", path = "../stanza", features = ["rfc_7395"] } web-sys = { version = "0.3", features = ["Request", "WebSocket"] } +js-sys = "0.3" wasm-bindgen = "0.2" [target.'cfg(not(target_arch = "wasm32"))'.dependencies] diff --git a/luz/src/connection/ws.rs b/luz/src/connection/ws.rs index fc983b1..74e3223 100644 --- a/luz/src/connection/ws.rs +++ b/luz/src/connection/ws.rs @@ -1,9 +1,10 @@ use tokio::sync::mpsc; +use tracing::debug; use wasm_bindgen::closure::Closure; use wasm_bindgen::JsCast; -use web_sys::WebSocket; +use web_sys::{ErrorEvent, WebSocket}; -use crate::Result; +use crate::error::ConnectionError; pub type Ws = WebSocket; @@ -11,10 +12,19 @@ pub type Ws = WebSocket; pub struct Connection(pub Ws); impl Connection { - pub async fn connect(server: impl AsRef<str> + std::fmt::Debug) -> Result<Self> { + pub async fn connect( + server: impl AsRef<str> + std::fmt::Debug, + ) -> Result<Self, ConnectionError> { // TODO: get the connection url here - let ws = WebSocket::new_with_str("wss://xmpp.bunny.garden/ws", "xmpp").unwrap(); - let (send, mut recv) = mpsc::unbounded_channel(); + debug!("creating websocket"); + let url = String::from("wss://xmpp.bunny.garden/ws"); + let ws = WebSocket::new_with_str(&url, "xmpp").map_err(|error| { + let error: js_sys::Error = error.into(); + let message = error.message().as_string().unwrap(); + ConnectionError::Create(url.clone(), message) + })?; + debug!("created websocket"); + let (send, mut open_recv) = mpsc::unbounded_channel(); let onopen = Closure::<dyn FnMut()>::new(Box::new(move || { tracing::info!("socket opened"); let send = send.clone(); @@ -25,9 +35,26 @@ impl Connection { } } }) as Box<dyn FnMut()>); + let (send, mut error_recv) = mpsc::unbounded_channel(); + let onerror = Closure::<dyn FnMut(_)>::new(Box::new(move |e: ErrorEvent| { + // error will always be 1006, so doesn't matter. + tracing::error!("connection error"); + match send.send(()) { + Ok(()) => (), + Err(e) => { + tracing::error!("socket error notify: {:?}", e); + } + } + }) as Box<dyn FnMut(_)>); ws.set_onopen(Some(onopen.as_ref().unchecked_ref())); + ws.set_onerror(Some(onerror.as_ref().unchecked_ref())); - recv.recv().await.unwrap(); + tokio::select! { + _error = error_recv.recv() => { Err(ConnectionError::Connect(url))? }, + Some(open) = open_recv.recv() => { }, + else => { Err(ConnectionError::Connect(url))? } + } + debug!("um what"); // TODO: check reply if it's xmpp too Ok(Self(ws)) diff --git a/luz/src/error.rs b/luz/src/error.rs index 6b08c15..3fcca57 100644 --- a/luz/src/error.rs +++ b/luz/src/error.rs @@ -13,6 +13,9 @@ pub enum Error { #[cfg(not(target_arch = "wasm32"))] #[error("connection")] Connection, + #[cfg(target_arch = "wasm32")] + #[error("connection: {0}")] + Connection(#[from] ConnectionError), #[error("utf8 decode: {0}")] Utf8Decode(#[from] Utf8Error), #[error("negotiation")] @@ -57,3 +60,12 @@ impl From<rsasl::prelude::SASLError> for SASLError { Self::SASL(Arc::new(e)) } } + +#[cfg(target_arch = "wasm32")] +#[derive(Error, Debug, Clone)] +pub enum ConnectionError { + #[error("creating websocket at \"{0}\": {1}")] + Create(String, String), + #[error("failed to connect to \"{0}\"")] + Connect(String), +} |