mirror of
https://github.com/cccb/servicepoint.git
synced 2025-01-18 18:10:14 +01:00
close connection on drop
This commit is contained in:
parent
5f8f691464
commit
366aec054f
|
@ -43,6 +43,10 @@ required-features = ["rand"]
|
||||||
name = "game_of_life"
|
name = "game_of_life"
|
||||||
required-features = ["rand"]
|
required-features = ["rand"]
|
||||||
|
|
||||||
|
[[example]]
|
||||||
|
name = "websocket"
|
||||||
|
required-features = ["protocol_websocket"]
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
# for examples
|
# for examples
|
||||||
clap = { version = "4.5", features = ["derive"] }
|
clap = { version = "4.5", features = ["derive"] }
|
||||||
|
|
27
crates/servicepoint/examples/websocket.rs
Normal file
27
crates/servicepoint/examples/websocket.rs
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
//! Example for how to use the WebSocket connection
|
||||||
|
|
||||||
|
use servicepoint::{
|
||||||
|
Command, CompressionCode, Connection, Grid, Origin, PixelGrid,
|
||||||
|
};
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
// make connection mut
|
||||||
|
let mut connection =
|
||||||
|
Connection::open_websocket("ws://localhost:8080".parse().unwrap())
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
// use send_mut instead of send
|
||||||
|
connection.send_mut(Command::Clear).unwrap();
|
||||||
|
|
||||||
|
let mut pixels = PixelGrid::max_sized();
|
||||||
|
pixels.fill(true);
|
||||||
|
|
||||||
|
// use send_mut instead of send
|
||||||
|
connection
|
||||||
|
.send_mut(Command::BitmapLinearWin(
|
||||||
|
Origin::ZERO,
|
||||||
|
pixels,
|
||||||
|
CompressionCode::Lzma,
|
||||||
|
))
|
||||||
|
.unwrap();
|
||||||
|
}
|
|
@ -111,7 +111,6 @@ impl Connection {
|
||||||
|
|
||||||
let request = ClientRequestBuilder::new(uri).into_client_request()?;
|
let request = ClientRequestBuilder::new(uri).into_client_request()?;
|
||||||
let (sock, _) = connect(request)?;
|
let (sock, _) = connect(request)?;
|
||||||
|
|
||||||
Ok(Self::WebSocket(sock))
|
Ok(Self::WebSocket(sock))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -208,6 +207,15 @@ impl Connection {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Drop for Connection {
|
||||||
|
fn drop(&mut self) {
|
||||||
|
#[cfg(feature = "protocol_websocket")]
|
||||||
|
if let Connection::WebSocket(sock) = self {
|
||||||
|
_ = sock.close(None);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
Loading…
Reference in a new issue