Skip to content

Commit

Permalink
end comments with full stop instead of colon
Browse files Browse the repository at this point in the history
(Better for consistency with the rest of axum's codebase.)
  • Loading branch information
hmeine committed Jan 6, 2023
1 parent 80d4d85 commit 6b795db
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions examples/chat/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};

// Our shared state
struct AppState {
user_set: Mutex<HashSet<String>>, // Currently taken user names (must be unique)
tx: broadcast::Sender<String>, // Receivers can be created via tx.subscribe()
// We require unique usernames. This tracks which usernames have been taken.
user_set: Mutex<HashSet<String>>,
// Channel used to send messages to all connected clients.
tx: broadcast::Sender<String>,
}

#[tokio::main]
Expand All @@ -40,7 +42,7 @@ async fn main() {
.with(tracing_subscriber::fmt::layer())
.init();

// Set up application state for use with with_state():
// Set up application state for use with with_state().
let user_set = Mutex::new(HashSet::new());
let (tx, _rx) = broadcast::channel(100);

Expand Down Expand Up @@ -70,7 +72,7 @@ async fn websocket_handler(
// connected client / user, for which we will spawn two independent tasks (for
// receiving / sending chat messages).
async fn websocket(stream: WebSocket, state: Arc<AppState>) {
// By splitting we can send and receive at the same time:
// By splitting, we can send and receive at the same time.
let (mut sender, mut receiver) = stream.split();

// Username gets set in the receive loop, if it's valid.
Expand All @@ -96,16 +98,16 @@ async fn websocket(stream: WebSocket, state: Arc<AppState>) {
}

// We subscribe *before* sending the "joined" message, so that we will also
// display it to our client:
// display it to our client.
let mut rx = state.tx.subscribe();

// Now send the "joined" message to all subscribers:
// Now send the "joined" message to all subscribers.
let msg = format!("{} joined.", username);
tracing::debug!("{}", msg);
let _ = state.tx.send(msg);

// Spawn the first task that will receive broadcast messages and send text
// messages over the websocket to our client:
// messages over the websocket to our client.
let mut send_task = tokio::spawn(async move {
while let Ok(msg) = rx.recv().await {
// In any websocket error, break loop.
Expand All @@ -115,31 +117,31 @@ async fn websocket(stream: WebSocket, state: Arc<AppState>) {
}
});

// Clone things we want to pass (move) to the receiving task:
// Clone things we want to pass (move) to the receiving task.
let tx = state.tx.clone();
let name = username.clone();

// Spawn a task that takes messages from the websocket, prepends the user
// name, and sends them to all broadcast subscribers:
// name, and sends them to all broadcast subscribers.
let mut recv_task = tokio::spawn(async move {
while let Some(Ok(Message::Text(text))) = receiver.next().await {
// Add username before message.
let _ = tx.send(format!("{}: {}", name, text));
}
});

// If any one of the tasks run to completion, we abort the other:
// If any one of the tasks run to completion, we abort the other.
tokio::select! {
_ = (&mut send_task) => recv_task.abort(),
_ = (&mut recv_task) => send_task.abort(),
};

// Send "user left" message (similar to "joined" above):
// Send "user left" message (similar to "joined" above).
let msg = format!("{} left.", username);
tracing::debug!("{}", msg);
let _ = state.tx.send(msg);

// Remove username from map so new clients can take it again:
// Remove username from map so new clients can take it again.
state.user_set.lock().unwrap().remove(&username);
}

Expand Down

0 comments on commit 6b795db

Please sign in to comment.