This commit is contained in:
Bastian Gruber 2023-04-29 14:15:06 +02:00
parent eda218c94b
commit 673e26e510
No known key found for this signature in database
GPG key ID: BE9F8C772B188CBF

View file

@ -21,7 +21,7 @@ type Message = String;
type Id = i32; type Id = i32;
#[derive(Clone, Debug, Default)] #[derive(Clone, Debug, Default)]
struct BroadcastMessage(Username, Message); struct BroadcastMessage(Id, Message);
#[derive(Clone, Debug, Default)] #[derive(Clone, Debug, Default)]
struct Users(Arc<Mutex<HashMap<Id, Username>>>); struct Users(Arc<Mutex<HashMap<Id, Username>>>);
@ -63,11 +63,11 @@ async fn main() -> Result<()> {
name = username.clone(); name = username.clone();
db.0.lock().unwrap().insert(id, username.clone()); db.0.lock().unwrap().insert(id, username.clone());
let message = compose_message(id, db.clone()); let message = compose_message(id, db.clone());
info!("Adding username: {username} to db"); info!("Adding username/id: {username}/{id} to db");
let _ = framed.send(message).await; let _ = framed.send(message).await;
info!("Send message to client"); info!("Send room message to {username}");
let b = BroadcastMessage( let b = BroadcastMessage(
username.clone(), id,
format!("* {} has entered the room", username), format!("* {} has entered the room", username),
); );
let _ = tx.send(b); let _ = tx.send(b);
@ -92,7 +92,7 @@ async fn main() -> Result<()> {
// broadcast message to all clients except the one who sent it // broadcast message to all clients except the one who sent it
info!("Receiving new chat message: {n}"); info!("Receiving new chat message: {n}");
let b = let b =
BroadcastMessage(name.clone(), format!("[{}]: {}", name, n)); BroadcastMessage(id, format!("[{}]: {}", name, n));
let _ = tx.send(b); let _ = tx.send(b);
} }
Some(Err(e)) => { Some(Err(e)) => {
@ -104,7 +104,7 @@ async fn main() -> Result<()> {
// send leave message // send leave message
info!("No next frame"); info!("No next frame");
let b = let b =
BroadcastMessage(name.clone(), format!("* {} has left the room", name)); BroadcastMessage(id, format!("* {} has left the room", name));
db.0.lock().unwrap().remove(&id); db.0.lock().unwrap().remove(&id);
let _ = tx.send(b); let _ = tx.send(b);
break; break;
@ -114,7 +114,7 @@ async fn main() -> Result<()> {
message = rx.recv() => { message = rx.recv() => {
let broadcast = message.clone().unwrap(); let broadcast = message.clone().unwrap();
info!("Broadcast received: {:?}", message.clone().unwrap()); info!("Broadcast received: {:?}", message.clone().unwrap());
if broadcast.0 != name { if broadcast.0 != id {
info!("Broadcast sent to {}: {:?}", name, message.clone().unwrap()); info!("Broadcast sent to {}: {:?}", name, message.clone().unwrap());
let _ = framed.send(message.unwrap().1).await; let _ = framed.send(message.unwrap().1).await;
} }