added voice chat websocket route
This commit is contained in:
+33
-3
@@ -1,3 +1,4 @@
|
||||
use axum::body::Bytes;
|
||||
use dashmap::DashMap;
|
||||
use std::sync::Arc;
|
||||
use tokio::sync::broadcast;
|
||||
@@ -6,18 +7,25 @@ use uuid::Uuid;
|
||||
use crate::routes::messages::Message;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Realtime {
|
||||
pub struct RealtimeMessages {
|
||||
pub clients: Arc<DashMap<Uuid, broadcast::Sender<Message>>>,
|
||||
}
|
||||
|
||||
impl Realtime {
|
||||
type VoicePacket = (Uuid, Bytes);
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct RealTimeVoices {
|
||||
pub rooms: Arc<DashMap<Uuid, broadcast::Sender<VoicePacket>>>,
|
||||
}
|
||||
|
||||
impl RealtimeMessages {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
clients: Arc::new(DashMap::new()),
|
||||
}
|
||||
}
|
||||
|
||||
/// Get or create the channel for a specific user
|
||||
/// Get or create the sender for a given user
|
||||
pub fn get_sender(&self, user_uuid: Uuid) -> broadcast::Sender<Message> {
|
||||
self.clients
|
||||
.entry(user_uuid)
|
||||
@@ -25,6 +33,7 @@ impl Realtime {
|
||||
.clone()
|
||||
}
|
||||
|
||||
/// Send a message to all the recipients
|
||||
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) {
|
||||
@@ -33,3 +42,24 @@ impl Realtime {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl RealTimeVoices {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
rooms: Arc::new(DashMap::new()),
|
||||
}
|
||||
}
|
||||
|
||||
/// Get or create the broadcast sender for a given room
|
||||
pub fn get_or_create_room(&self, room_uuid: Uuid) -> broadcast::Sender<VoicePacket> {
|
||||
self.rooms
|
||||
.entry(room_uuid)
|
||||
.or_insert_with(|| broadcast::channel(500).0)
|
||||
.clone()
|
||||
}
|
||||
|
||||
/// Clean up empty rooms
|
||||
pub fn retain_active(&self) {
|
||||
self.rooms.retain(|_, sender| sender.receiver_count() > 0);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user