Revert id instead of name
This commit is contained in:
parent
673e26e510
commit
299a4b3b75
1 changed files with 16 additions and 18 deletions
|
|
@ -18,13 +18,13 @@ type Result<T> = std::result::Result<T, Error>;
|
||||||
|
|
||||||
type Username = String;
|
type Username = String;
|
||||||
type Message = String;
|
type Message = String;
|
||||||
type Id = i32;
|
type Address = SocketAddr;
|
||||||
|
|
||||||
#[derive(Clone, Debug, Default)]
|
#[derive(Clone, Debug, Default)]
|
||||||
struct BroadcastMessage(Id, Message);
|
struct BroadcastMessage(Username, Message);
|
||||||
|
|
||||||
#[derive(Clone, Debug, Default)]
|
#[derive(Clone, Debug, Default)]
|
||||||
struct Users(Arc<Mutex<HashMap<Id, Username>>>);
|
struct Users(Arc<Mutex<HashMap<Username, Address>>>);
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() -> Result<()> {
|
async fn main() -> Result<()> {
|
||||||
|
|
@ -36,7 +36,6 @@ async fn main() -> Result<()> {
|
||||||
let (tx, _) = broadcast::channel(256);
|
let (tx, _) = broadcast::channel(256);
|
||||||
|
|
||||||
let db = Users::default();
|
let db = Users::default();
|
||||||
let mut id = 0;
|
|
||||||
|
|
||||||
// Infinite loop to always listen to new connections on this IP/PORT
|
// Infinite loop to always listen to new connections on this IP/PORT
|
||||||
loop {
|
loop {
|
||||||
|
|
@ -52,7 +51,6 @@ async fn main() -> Result<()> {
|
||||||
.await;
|
.await;
|
||||||
|
|
||||||
let mut name = String::default();
|
let mut name = String::default();
|
||||||
id += 1;
|
|
||||||
|
|
||||||
// We read exactly one line per loop. A line ends with \n.
|
// We read exactly one line per loop. A line ends with \n.
|
||||||
// So if the client doesn't frame their package with \n at the end,
|
// So if the client doesn't frame their package with \n at the end,
|
||||||
|
|
@ -61,13 +59,13 @@ async fn main() -> Result<()> {
|
||||||
Some(Ok(username)) => {
|
Some(Ok(username)) => {
|
||||||
if !username.is_empty() && username.is_ascii() {
|
if !username.is_empty() && username.is_ascii() {
|
||||||
name = username.clone();
|
name = username.clone();
|
||||||
db.0.lock().unwrap().insert(id, username.clone());
|
db.0.lock().unwrap().insert(username.clone(), address);
|
||||||
let message = compose_message(id, db.clone());
|
let message = compose_message(username.clone(), db.clone());
|
||||||
info!("Adding username/id: {username}/{id} to db");
|
info!("Adding username: {username} to db");
|
||||||
let _ = framed.send(message).await;
|
let _ = framed.send(message).await;
|
||||||
info!("Send room message to {username}");
|
info!("Send message to client");
|
||||||
let b = BroadcastMessage(
|
let b = BroadcastMessage(
|
||||||
id,
|
username.clone(),
|
||||||
format!("* {} has entered the room", username),
|
format!("* {} has entered the room", username),
|
||||||
);
|
);
|
||||||
let _ = tx.send(b);
|
let _ = tx.send(b);
|
||||||
|
|
@ -92,7 +90,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(id, format!("[{}]: {}", name, n));
|
BroadcastMessage(name.clone(), format!("[{}]: {}", name, n));
|
||||||
let _ = tx.send(b);
|
let _ = tx.send(b);
|
||||||
}
|
}
|
||||||
Some(Err(e)) => {
|
Some(Err(e)) => {
|
||||||
|
|
@ -104,8 +102,8 @@ async fn main() -> Result<()> {
|
||||||
// send leave message
|
// send leave message
|
||||||
info!("No next frame");
|
info!("No next frame");
|
||||||
let b =
|
let b =
|
||||||
BroadcastMessage(id, format!("* {} has left the room", name));
|
BroadcastMessage(name.clone(), format!("* {} has left the room", name));
|
||||||
db.0.lock().unwrap().remove(&id);
|
db.0.lock().unwrap().remove(&name.clone());
|
||||||
let _ = tx.send(b);
|
let _ = tx.send(b);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
@ -114,7 +112,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 != id {
|
if broadcast.0 != name {
|
||||||
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;
|
||||||
}
|
}
|
||||||
|
|
@ -126,14 +124,14 @@ async fn main() -> Result<()> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn compose_message(id: i32, db: Users) -> String {
|
fn compose_message(name: String, db: Users) -> String {
|
||||||
format!(
|
format!(
|
||||||
"* The room contains: {}",
|
"* The room contains: {}",
|
||||||
db.0.lock()
|
db.0.lock()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.iter()
|
.keys()
|
||||||
.filter(|(i, _)| **i != id)
|
.filter(|n| n.as_str() != name)
|
||||||
.map(|(_, n)| n.to_string())
|
.map(|n| n.to_string())
|
||||||
.collect::<Vec<_>>()
|
.collect::<Vec<_>>()
|
||||||
.join(", ")
|
.join(", ")
|
||||||
)
|
)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue