diff --git a/server/src/websocket.rs b/server/src/websocket.rs index 7d78df1..a101862 100644 --- a/server/src/websocket.rs +++ b/server/src/websocket.rs @@ -37,20 +37,29 @@ pub async fn websocket_on_connection(stream: WebSocket, state: Arc, ad let mut map = HashMap::new(); map.insert(addr, dm_tx.clone()); + // Roll for username and re-roll if taken + let mut username; + + loop { + username = format!( + "{} {}", + state.first_names.choose(&mut rand::thread_rng()).unwrap(), + state.last_names.choose(&mut rand::thread_rng()).unwrap(), + ); + + if !state.reserved_names.read().unwrap().contains(&username) { + break; + } + } + state .users_tx .send(UserHandlerMessage::NewUser( - User::new( - format!( - "{} {}", - state.first_names.choose(&mut rand::thread_rng()).unwrap(), - state.last_names.choose(&mut rand::thread_rng()).unwrap(), - ), - dm_tx.clone(), - ), + User::new(username, dm_tx.clone()), addr, )) - .await.expect("User handler is down"); + .await + .expect("User handler is down"); // Subscribe to receive from global broadcast channel let mut rx = state.broadcast_tx.subscribe();