Skip to content

Commit

Permalink
Update docs and tests for close event
Browse files Browse the repository at this point in the history
  • Loading branch information
scratchyone committed Nov 25, 2022
1 parent d4ac143 commit 56484f8
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 9 deletions.
8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "wasm-sockets"
version = "0.2.2"
version = "0.3.0"
authors = ["scratchyone <[email protected]>"]
edition = "2018"
license = "MIT"
Expand All @@ -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

Expand Down
2 changes: 1 addition & 1 deletion Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion examples/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
46 changes: 44 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -203,6 +203,24 @@ impl PollingClient {
pub fn send_binary(&self, message: Vec<u8>) -> 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)]
Expand Down Expand Up @@ -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");
/// })));
/// ```
Expand All @@ -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),
}
}
}
2 changes: 1 addition & 1 deletion src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 56484f8

Please sign in to comment.