mirror of
https://github.com/csehviktor/status-monitor.git
synced 2025-08-08 18:06:14 +02:00
28 lines
651 B
Rust
28 lines
651 B
Rust
use common::StatusMessage;
|
|
use tokio::sync::{mpsc::Sender, Mutex};
|
|
|
|
pub struct ClientManager {
|
|
clients: Mutex<Vec<Sender<StatusMessage>>>,
|
|
}
|
|
|
|
impl ClientManager {
|
|
pub fn new() -> Self {
|
|
Self {
|
|
clients: Mutex::new(Vec::new()),
|
|
}
|
|
}
|
|
|
|
pub async fn add_client(&self, client: Sender<StatusMessage>) {
|
|
let mut clients = self.clients.lock().await;
|
|
clients.push(client);
|
|
}
|
|
|
|
pub async fn broadcast(&self, message: &StatusMessage) {
|
|
let mut clients = self.clients.lock().await;
|
|
|
|
clients.retain(|client| {
|
|
client.try_send(message.clone()).is_ok()
|
|
});
|
|
}
|
|
}
|