diff --git a/russh/examples/echoserver.rs b/russh/examples/echoserver.rs index 18d96718..26618d19 100644 --- a/russh/examples/echoserver.rs +++ b/russh/examples/echoserver.rs @@ -38,14 +38,14 @@ async fn main() { #[derive(Clone)] struct Server { - clients: Arc>>, + clients: Arc>>, id: usize, } impl Server { async fn post(&mut self, data: CryptoVec) { let mut clients = self.clients.lock().await; - for ((id, channel), ref mut s) in clients.iter_mut() { + for (id, (channel, ref mut s)) in clients.iter_mut() { if *id != self.id { let _ = s.data(*channel, data.clone()).await; } @@ -76,7 +76,7 @@ impl server::Handler for Server { ) -> Result { { let mut clients = self.clients.lock().await; - clients.insert((self.id, channel.id()), session.handle()); + clients.insert(self.id, (channel.id(), session.handle())); } Ok(true) } @@ -135,3 +135,14 @@ impl server::Handler for Server { Ok(true) } } + +impl Drop for Server { + fn drop(&mut self) { + let id = self.id; + let clients = self.clients.clone(); + tokio::spawn(async move { + let mut clients = clients.lock().await; + clients.remove(&id); + }); + } +} diff --git a/russh/examples/ratatui_app.rs b/russh/examples/ratatui_app.rs index d5531fd7..13454b26 100644 --- a/russh/examples/ratatui_app.rs +++ b/russh/examples/ratatui_app.rs @@ -208,6 +208,17 @@ impl Handler for AppServer { } } +impl Drop for AppServer { + fn drop(&mut self) { + let id = self.id; + let clients = self.clients.clone(); + tokio::spawn(async move { + let mut clients = clients.lock().await; + clients.remove(&id); + }); + } +} + #[tokio::main] async fn main() { let mut server = AppServer::new(); diff --git a/russh/examples/ratatui_shared_app.rs b/russh/examples/ratatui_shared_app.rs index eedaee5e..e09e745f 100644 --- a/russh/examples/ratatui_shared_app.rs +++ b/russh/examples/ratatui_shared_app.rs @@ -207,6 +207,17 @@ impl Handler for AppServer { } } +impl Drop for AppServer { + fn drop(&mut self) { + let id = self.id; + let clients = self.clients.clone(); + tokio::spawn(async move { + let mut clients = clients.lock().await; + clients.remove(&id); + }); + } +} + #[tokio::main] async fn main() { let mut server = AppServer::new();