chore: Impl AsRef for ServerEvent

Signed-off-by: Awiteb <a@4rs.nl>
This commit is contained in:
Awiteb 2024-07-09 19:23:49 +03:00
parent 2bac5be8c8
commit 33b44cb256
Signed by: awiteb
GPG key ID: 3F6B55640AA6682F
3 changed files with 21 additions and 10 deletions

View file

@ -19,7 +19,7 @@ use std::sync::Arc;
use chrono::Utc; use chrono::Utc;
use oxidetalis_config::Config; use oxidetalis_config::Config;
use rayon::iter::{IntoParallelRefMutIterator, ParallelIterator}; use rayon::iter::{IntoParallelRefMutIterator, ParallelIterator};
use salvo::{websocket::Message, Depot}; use salvo::Depot;
use sea_orm::DatabaseConnection; use sea_orm::DatabaseConnection;
use uuid::Uuid; use uuid::Uuid;
@ -87,9 +87,10 @@ impl OnlineUsersExt for OnlineUsers {
let now = Utc::now(); let now = Utc::now();
self.write().await.par_iter_mut().for_each(|(_, u)| { self.write().await.par_iter_mut().for_each(|(_, u)| {
u.pinged_at = now; u.pinged_at = now;
let _ = u.sender.unbounded_send(Ok(Message::from( let _ = u.sender.unbounded_send(Ok(ServerEvent::ping()
&ServerEvent::ping().sign(&u.shared_secret), .sign(&u.shared_secret)
))); .as_ref()
.into()));
}); });
} }

View file

@ -101,6 +101,12 @@ impl ServerEvent<Unsigned> {
} }
} }
impl<S> AsRef<Self> for ServerEvent<S> {
fn as_ref(&self) -> &Self {
self
}
}
impl From<&ServerEvent<Signed>> for Message { impl From<&ServerEvent<Signed>> for Message {
fn from(value: &ServerEvent<Signed>) -> Self { fn from(value: &ServerEvent<Signed>) -> Self {
Message::text(serde_json::to_string(value).expect("This can't fail")) Message::text(serde_json::to_string(value).expect("This can't fail"))

View file

@ -132,18 +132,22 @@ async fn handle_socket(
match handle_ws_msg(msg, &nonce_cache, &user_shared_secret).await { match handle_ws_msg(msg, &nonce_cache, &user_shared_secret).await {
Ok(event) => { Ok(event) => {
if let Some(server_event) = handle_events(event, &conn_id).await { if let Some(server_event) = handle_events(event, &conn_id).await {
if let Err(err) = sender.unbounded_send(Ok(Message::from( if let Err(err) = sender.unbounded_send(Ok(server_event
&server_event.sign(&user_shared_secret), .sign(&user_shared_secret)
))) { .as_ref()
.into()))
{
log::error!("Websocket Error: {err}"); log::error!("Websocket Error: {err}");
break; break;
} }
}; };
} }
Err(err) => { Err(err) => {
if let Err(err) = sender.unbounded_send(Ok(Message::from( if let Err(err) = sender.unbounded_send(Ok(ServerEvent::from(err)
&ServerEvent::from(err).sign(&user_shared_secret), .sign(&user_shared_secret)
))) { .as_ref()
.into()))
{
log::error!("Websocket Error: {err}"); log::error!("Websocket Error: {err}");
break; break;
}; };