Skip to content

Commit

Permalink
Merge pull request #4 from vpzomtrrfrt/close
Browse files Browse the repository at this point in the history
Add close methods
  • Loading branch information
scratchyone authored Nov 25, 2022
2 parents cbac59d + 1494594 commit e87e504
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 4 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
42 changes: 42 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
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 @@ -435,4 +453,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),
}
}
}

0 comments on commit e87e504

Please sign in to comment.