Track nicknames

This commit is contained in:
Yash Karandikar 2021-12-08 15:22:01 -06:00
parent 20131f6d8e
commit e087a308a0

View file

@ -78,6 +78,13 @@ async fn connection_read(
.await
.unwrap();
println!("ID: {}", id);
let mut nick = String::from(id.split("-").collect::<Vec<&str>>()[0]);
sender
.send(Message {
msg: format!("{} has joined the chat\n", nick),
sender: id.clone(),
})
.unwrap();
*current_client_id.lock().await = Some(id.clone());
loop {
let mut buf = [0u8; 1024];
@ -100,10 +107,20 @@ async fn connection_read(
let res = String::from_utf8_lossy(&buf[..bytes]).to_string();
print!("{}", res);
if res.starts_with("MSG") {
sender.send(Message { msg: res, sender: id.clone() }).unwrap();
sender.send(Message { msg: format!("{} {}", nick, res.clone()) , sender: id.clone() }).unwrap();
} else if res.starts_with("NICK") {
let old_nick = nick.clone();
nick = res[5..].trim().to_string();
sender.send(Message { msg: format!("{} {}", old_nick, res.clone()), sender: id.clone()}).unwrap();
}
}
}
}
sender
.send(Message {
msg: format!("{} has disconnected\n", nick),
sender: id.clone(),
})
.unwrap();
println!("Client {} disconnected", id);
}