From d4ac143058d2f5a33afce36320e5d6a9a611325d Mon Sep 17 00:00:00 2001 From: Colin Reeder Date: Sun, 6 Mar 2022 16:13:29 -0700 Subject: [PATCH 1/2] Include CloseEvent in on_close call --- Cargo.toml | 1 + src/lib.rs | 17 +++++++++-------- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 7ecdb29..ddd23ea 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -33,6 +33,7 @@ js-sys = "0.3" web-sys = { version = "0.3.22", features = [ "BinaryType", "Blob", + "CloseEvent", "ErrorEvent", "FileReader", "MessageEvent", diff --git a/src/lib.rs b/src/lib.rs index dfbc14e..9f50474 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -94,7 +94,7 @@ use thiserror::Error; use wasm_bindgen::prelude::*; #[cfg(target_arch = "wasm32")] use wasm_bindgen::JsCast; -use web_sys::{ErrorEvent, MessageEvent, WebSocket}; +use web_sys::{CloseEvent, ErrorEvent, MessageEvent, WebSocket}; #[cfg(not(target_arch = "wasm32"))] compile_error!("wasm-sockets can only compile to WASM targets"); @@ -158,7 +158,7 @@ impl PollingClient { let status_ref = status.clone(); - client.set_on_close(Some(Box::new(move || { + client.set_on_close(Some(Box::new(move |_evt| { *status_ref.borrow_mut() = ConnectionStatus::Disconnected; }))); @@ -227,7 +227,7 @@ pub struct EventClient { /// The function bound to the on_message event pub on_message: Rc ()>>>>, /// The function bound to the on_close event - pub on_close: Rc ()>>>>, + pub on_close: Rc ()>>>>, } #[cfg(target_arch = "wasm32")] @@ -265,16 +265,17 @@ impl EventClient { ws.set_onerror(Some(onerror_callback.as_ref().unchecked_ref())); onerror_callback.forget(); - let on_close: Rc ()>>>> = Rc::new(RefCell::new(None)); + let on_close: Rc ()>>>> = + Rc::new(RefCell::new(None)); let on_close_ref = on_close.clone(); let ref_status = status.clone(); - let onclose_callback = Closure::wrap(Box::new(move || { + let onclose_callback = Closure::wrap(Box::new(move |e: CloseEvent| { *ref_status.borrow_mut() = ConnectionStatus::Disconnected; if let Some(f) = &*on_close_ref.borrow() { - f.as_ref()(); + f.as_ref()(e); } - }) as Box); + }) as Box); ws.set_onclose(Some(onclose_callback.as_ref().unchecked_ref())); onclose_callback.forget(); @@ -415,7 +416,7 @@ impl EventClient { /// info!("Closed"); /// }))); /// ``` - pub fn set_on_close(&mut self, f: Option ()>>) { + pub fn set_on_close(&mut self, f: Option ()>>) { *self.on_close.borrow_mut() = f; } From 56484f8e4c0f493eb86909a59a153ce39c872faf Mon Sep 17 00:00:00 2001 From: scratchyone Date: Fri, 25 Nov 2022 12:57:56 -0500 Subject: [PATCH 2/2] Update docs and tests for close event --- Cargo.toml | 8 ++++---- Readme.md | 2 +- examples/event.rs | 2 +- src/lib.rs | 46 ++++++++++++++++++++++++++++++++++++++++++++-- src/tests.rs | 2 +- 5 files changed, 51 insertions(+), 9 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index ddd23ea..ff31ff0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "wasm-sockets" -version = "0.2.2" +version = "0.3.0" authors = ["scratchyone "] edition = "2018" license = "MIT" @@ -14,12 +14,12 @@ description = "A WASM-only websocket library" repository = "https://github.com/scratchyone/wasm-sockets" readme = "Readme.md" -[lib] -crate-type = ["cdylib", "rlib"] - [package.metadata.docs.rs] targets = ["wasm32-unknown-unknown"] +[lib] +crate-type = ["cdylib", "rlib"] + # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/Readme.md b/Readme.md index 1008893..6c75c27 100644 --- a/Readme.md +++ b/Readme.md @@ -29,7 +29,7 @@ fn main() -> Result<(), WebSocketError> { client.send_string("Hello, World!").unwrap(); client.send_binary(vec![20]).unwrap(); }))); - client.set_on_close(Some(Box::new(|| { + client.set_on_close(Some(Box::new(|_evt| { info!("Connection closed"); }))); client.set_on_message(Some(Box::new( diff --git a/examples/event.rs b/examples/event.rs index f97bfbe..c1fb317 100644 --- a/examples/event.rs +++ b/examples/event.rs @@ -21,7 +21,7 @@ fn main() -> Result<(), WebSocketError> { client.send_string("Hello, World!").unwrap(); client.send_binary(vec![20]).unwrap(); }))); - client.set_on_close(Some(Box::new(|| { + client.set_on_close(Some(Box::new(|_evt| { info!("Connection closed"); }))); client.set_on_message(Some(Box::new( diff --git a/src/lib.rs b/src/lib.rs index 9f50474..247fdb3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -24,7 +24,7 @@ //! client.send_string("Hello, World!").unwrap(); //! client.send_binary(vec![20]).unwrap(); //! }))); -//! client.set_on_close(Some(Box::new(|| { +//! client.set_on_close(Some(Box::new(|_evt| { //! info!("Connection closed"); //! }))); //! client.set_on_message(Some(Box::new( @@ -203,6 +203,24 @@ impl PollingClient { pub fn send_binary(&self, message: Vec) -> Result<(), JsValue> { self.event_client.send_binary(message) } + + /// Close the connection + /// ``` + /// client.close()?; + /// ``` + pub fn close(&self) -> Result<(), JsValue> { + self.event_client.close() + } + /// Close the connection with a custom close code and, optionally, a reason string + /// + /// The reason string must be at most 123 bytes long. + /// + /// ``` + /// client.close_with(1001, Some("going away"))?; + /// ``` + pub fn close_with(&self, code: u16, reason: Option<&str>) -> Result<(), JsValue> { + self.event_client.close_with(code, reason) + } } #[derive(Debug, Clone, Error)] @@ -412,7 +430,7 @@ impl EventClient { /// This will overwrite the previous handler. /// You can set [None](std::option) to disable the on_close handler. /// ``` - /// client.set_on_close(Some(Box::new(|| { + /// client.set_on_close(Some(Box::new(|_evt| { /// info!("Closed"); /// }))); /// ``` @@ -436,4 +454,28 @@ impl EventClient { .borrow() .send_with_u8_array(message.as_slice()) } + + /// Close the connection + /// ``` + /// client.close()?; + /// ``` + pub fn close(&self) -> Result<(), JsValue> { + self.connection.borrow().close() + } + /// Close the connection with a custom close code and, optionally, a reason string + /// + /// The reason string must be at most 123 bytes long. + /// + /// ``` + /// client.close_with(1001, Some("going away"))?; + /// ``` + pub fn close_with(&self, code: u16, reason: Option<&str>) -> Result<(), JsValue> { + match reason { + Some(reason) => self + .connection + .borrow() + .close_with_code_and_reason(code, reason), + None => self.connection.borrow().close_with_code(code), + } + } } diff --git a/src/tests.rs b/src/tests.rs index b04b537..b230af1 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -28,7 +28,7 @@ fn event() { client.send_string("Hello, World!").unwrap(); client.send_binary(vec![20]).unwrap(); }))); - client.set_on_close(Some(Box::new(|| { + client.set_on_close(Some(Box::new(|_evt| { info!("Connection closed"); }))); client.set_on_message(Some(Box::new(