refactor: clients now have a single websocket that handles all rooms the user is in

This commit is contained in:
2026-01-16 11:35:07 +01:00
parent 376353833c
commit 37e6bb25fc
8 changed files with 74 additions and 66 deletions
+15 -7
View File
@@ -1,27 +1,35 @@
use dashmap::DashMap;
use std::sync::Arc;
use tokio::sync::broadcast;
use uuid::Uuid;
use crate::routes::messages::Message;
pub type RoomId = i32;
#[derive(Clone)]
pub struct Realtime {
pub rooms: Arc<DashMap<RoomId, broadcast::Sender<Message>>>,
pub clients: Arc<DashMap<Uuid, broadcast::Sender<Message>>>,
}
impl Realtime {
pub fn new() -> Self {
Self {
rooms: Arc::new(DashMap::new()),
clients: Arc::new(DashMap::new()),
}
}
pub fn sender_for(&self, room: RoomId) -> broadcast::Sender<Message> {
self.rooms
.entry(room)
/// Get or create the channel for a specific user
pub fn get_sender(&self, user_uuid: Uuid) -> broadcast::Sender<Message> {
self.clients
.entry(user_uuid)
.or_insert_with(|| broadcast::channel(100).0)
.clone()
}
pub fn broadcast(&self, recipient_uuids: Vec<Uuid>, message: Message) {
for user_uuid in recipient_uuids {
if let Some(sender) = self.clients.get(&user_uuid) {
let _ = sender.send(message.clone());
}
}
}
}