Fix ordering of messages

This commit is contained in:
Bastian Gruber 2023-04-29 18:34:47 +02:00
parent ae52b25baa
commit c6a8c417ef
No known key found for this signature in database
GPG key ID: BE9F8C772B188CBF

View file

@ -40,7 +40,7 @@ async fn main() -> Result<()> {
// Infinite loop to always listen to new connections on this IP/PORT
loop {
let (stream, address) = listener.accept().await?;
let (tx, mut rx) = (tx.clone(), tx.subscribe());
let (tx, rx) = (tx.clone(), tx.subscribe());
let db = db.clone();
tokio::spawn(async move {
@ -63,12 +63,6 @@ async fn main() -> Result<()> {
let message = compose_message(username.clone(), db.clone());
info!("Adding username: {username} to db");
let _ = framed.send(message).await;
info!("Send message to client");
let b = BroadcastMessage(
username.clone(),
format!("* {} has entered the room", username),
);
let _ = tx.send(b);
} else {
return;
}
@ -83,6 +77,13 @@ async fn main() -> Result<()> {
}
}
let b = BroadcastMessage(
name.clone(),
format!("* {} has entered the room", name),
);
let _ = tx.send(b);
let mut rx = rx.resubscribe();
loop {
tokio::select! {
n = framed.next() => {