fix: Handle the websocet in handle_socket
itself
I have removed the task spwaning in `handle_socket` function, because Salvo already spawn `handle_socket` function in a new task when upgrading the connection to websocket. So, there is no need to spawn a new task in `handle_socket` function. refs: - Salvo task spawning: https://github.com/salvo-rs/salvo/blob/main/crates/extra/src/websocket.rs#L247-L256 Reviewed-on: #34 Reviewed-by: Amjad Alsharafi <me@amjad.alsharafi.dev> Signed-off-by: Awiteb <a@4rs.nl>
This commit is contained in:
parent
b4cac39157
commit
e61de9a8b1
1 changed files with 24 additions and 29 deletions
|
@ -148,38 +148,33 @@ async fn handle_socket(
|
|||
// TODO: Send the incoming chat request to the user, while they are offline.
|
||||
// This after adding last_login col to the user table
|
||||
|
||||
let fut = async move {
|
||||
while let Some(Ok(msg)) = user_ws_receiver.next().await {
|
||||
match handle_ws_msg(msg, &nonce_cache, &user_shared_secret).await {
|
||||
Ok(event) => {
|
||||
if let Some(server_event) =
|
||||
handle_events(event, &db_conn, &conn_id, user.as_ref()).await
|
||||
{
|
||||
if let Err(err) = sender.unbounded_send(Ok(server_event
|
||||
.sign(&user_shared_secret)
|
||||
.as_ref()
|
||||
.into()))
|
||||
{
|
||||
log::error!("Websocket Error: {err}");
|
||||
break;
|
||||
}
|
||||
};
|
||||
}
|
||||
Err(err) => {
|
||||
if let Err(err) = sender.unbounded_send(Ok(ServerEvent::from(err)
|
||||
.sign(&user_shared_secret)
|
||||
.as_ref()
|
||||
.into()))
|
||||
while let Some(Ok(msg)) = user_ws_receiver.next().await {
|
||||
match handle_ws_msg(msg, &nonce_cache, &user_shared_secret).await {
|
||||
Ok(event) => {
|
||||
if let Some(server_event) =
|
||||
handle_events(event, &db_conn, &conn_id, user.as_ref()).await
|
||||
{
|
||||
if let Err(err) = sender
|
||||
.unbounded_send(Ok(server_event.sign(&user_shared_secret).as_ref().into()))
|
||||
{
|
||||
log::error!("Websocket Error: {err}");
|
||||
break;
|
||||
};
|
||||
}
|
||||
};
|
||||
}
|
||||
user_disconnected(&db_conn, &conn_id, &user_public_key, user).await;
|
||||
};
|
||||
tokio_spawn(fut);
|
||||
}
|
||||
};
|
||||
}
|
||||
Err(err) => {
|
||||
if let Err(err) = sender.unbounded_send(Ok(ServerEvent::from(err)
|
||||
.sign(&user_shared_secret)
|
||||
.as_ref()
|
||||
.into()))
|
||||
{
|
||||
log::error!("Websocket Error: {err}");
|
||||
break;
|
||||
};
|
||||
}
|
||||
};
|
||||
}
|
||||
user_disconnected(&db_conn, &conn_id, &user_public_key, user).await;
|
||||
}
|
||||
|
||||
/// Handle websocket msg
|
||||
|
|
Loading…
Reference in a new issue