Ready to implement sync client manager

This commit is contained in:
2025-02-11 22:20:23 +01:00
parent bb1e412d36
commit 17e086b43c
6 changed files with 39 additions and 20 deletions

View File

@ -1,7 +1,6 @@
use crate::constants::{WS_CLIENT_TIMEOUT, WS_HEARTBEAT_INTERVAL};
use crate::extractors::client_auth::APIClientAuth;
use crate::server::HttpResult;
use crate::user::{APIClientID, UserID};
use actix_web::dev::Payload;
use actix_web::{web, FromRequest, HttpRequest};
use actix_ws::Message;
@ -11,21 +10,15 @@ use tokio::select;
use tokio::sync::broadcast;
use tokio::sync::broadcast::Receiver;
use tokio::time::interval;
use crate::broadcast_messages::BroadcastMessage;
/// WebSocket message
#[derive(Debug, Clone)]
pub enum WsMessage {
/// Request to close the session of a specific client
CloseClientSession(APIClientID),
/// Close all the sessions of a given user
CloseAllUserSessions(UserID),
}
/// Main WS route
pub async fn ws(
req: HttpRequest,
stream: web::Payload,
tx: web::Data<broadcast::Sender<WsMessage>>,
tx: web::Data<broadcast::Sender<BroadcastMessage>>,
) -> HttpResult {
// Forcefully ignore request payload by manually extracting authentication information
let auth = APIClientAuth::from_request(&req, &mut Payload::None).await?;
@ -44,7 +37,7 @@ pub async fn ws_handler(
mut session: actix_ws::Session,
mut msg_stream: actix_ws::MessageStream,
auth: APIClientAuth,
mut rx: Receiver<WsMessage>,
mut rx: Receiver<BroadcastMessage>,
) {
log::info!("WS connected");
@ -64,7 +57,7 @@ pub async fn ws_handler(
};
match msg {
WsMessage::CloseClientSession(id) => {
BroadcastMessage::CloseClientSession(id) => {
if let Some(client) = &auth.client {
if client.id == id {
log::info!(
@ -74,7 +67,7 @@ pub async fn ws_handler(
}
}
},
WsMessage::CloseAllUserSessions(userid) => {
BroadcastMessage::CloseAllUserSessions(userid) => {
if userid == auth.user.user_id {
log::info!(
"closing WS session of user {userid:?} as requested"
@ -82,7 +75,7 @@ pub async fn ws_handler(
break None;
}
}
};
_ => {}};
}