Element messages to back to websocket

This commit is contained in:
2025-02-25 19:52:42 +01:00
parent b91b1ba096
commit 53186d2e24
3 changed files with 60 additions and 3 deletions

View File

@ -1,4 +1,4 @@
use crate::broadcast_messages::BroadcastMessage;
use crate::broadcast_messages::{BroadcastMessage, SyncEvent};
use crate::constants::{WS_CLIENT_TIMEOUT, WS_HEARTBEAT_INTERVAL};
use crate::extractors::client_auth::APIClientAuth;
use crate::server::HttpResult;
@ -12,6 +12,13 @@ use tokio::sync::broadcast;
use tokio::sync::broadcast::Receiver;
use tokio::time::interval;
/// Messages send to the client
#[derive(Debug, serde::Deserialize, serde::Serialize)]
#[serde(tag = "type")]
pub enum WsMessage {
Sync(SyncEvent),
}
/// Main WS route
pub async fn ws(
req: HttpRequest,
@ -80,6 +87,19 @@ pub async fn ws_handler(
break None;
}
}
BroadcastMessage::SyncEvent(userid, event) => {
if userid != auth.user.user_id {
continue;
}
// Send the message to the websocket
if let Ok(msg) = serde_json::to_string(&WsMessage::Sync(event)) {
if let Err(e) = session.text(msg).await {
log::error!("Failed to send SyncEvent: {}", e);
}
}
}
_ => {}};
}