This commit is contained in:
Adam 2024-08-06 02:26:57 -04:00
parent 5ca0c8250f
commit 63571c1bfa

View file

@ -39,6 +39,7 @@ pub async fn websocket_on_connection(stream: WebSocket, state: Arc<AppState>, ad
// Split channels to send and receive asynchronously.
let (mut sender, mut receiver) = stream.split();
// Create channel for direct messages
let (dm_tx, mut dm_rx) = mpsc::channel(30);
let _ = state
@ -55,13 +56,15 @@ pub async fn websocket_on_connection(stream: WebSocket, state: Arc<AppState>, ad
// Send messages to this client
let mut send_task = tokio::spawn(async move {
loop {
// Global broadcast channel
while let Ok(msg) = rx.recv().await {
if sender.send(Message::Text(msg)).await.is_err() {
break;
}
}
while let Some(message) = dm_rx.recv().await {
if sender.send(message).await.is_err() {
// Direct message channel
while let Some(msg) = dm_rx.recv().await {
if sender.send(msg).await.is_err() {
break;
}
}