From 56484f8e4c0f493eb86909a59a153ce39c872faf Mon Sep 17 00:00:00 2001 From: scratchyone Date: Fri, 25 Nov 2022 12:57:56 -0500 Subject: [PATCH] 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(