mirror of
https://github.com/actix/examples
synced 2025-06-26 17:17:42 +02:00
Use captured args in format string (#558)
This commit is contained in:
@ -75,9 +75,8 @@ impl Handler<JoinRoom> for WsChatServer {
|
||||
|
||||
let id = self.add_client_to_room(&room_name, None, client);
|
||||
let join_msg = format!(
|
||||
"{} joined {}",
|
||||
"{} joined {room_name}",
|
||||
client_name.unwrap_or_else(|| "anon".to_string()),
|
||||
room_name
|
||||
);
|
||||
|
||||
self.send_chat_message(&room_name, &join_msg, id);
|
||||
|
@ -63,9 +63,8 @@ impl WsChatSession {
|
||||
|
||||
pub fn send_msg(&self, msg: &str) {
|
||||
let content = format!(
|
||||
"{}: {}",
|
||||
"{}: {msg}",
|
||||
self.name.clone().unwrap_or_else(|| "anon".to_string()),
|
||||
msg
|
||||
);
|
||||
|
||||
let msg = SendMessage(self.room.clone(), self.id, content);
|
||||
@ -110,7 +109,7 @@ impl StreamHandler<Result<ws::Message, ws::ProtocolError>> for WsChatSession {
|
||||
Ok(msg) => msg,
|
||||
};
|
||||
|
||||
log::debug!("WEBSOCKET MESSAGE: {:?}", msg);
|
||||
log::debug!("WEBSOCKET MESSAGE: {msg:?}");
|
||||
|
||||
match msg {
|
||||
ws::Message::Text(text) => {
|
||||
@ -133,13 +132,13 @@ impl StreamHandler<Result<ws::Message, ws::ProtocolError>> for WsChatSession {
|
||||
Some("/name") => {
|
||||
if let Some(name) = command.next() {
|
||||
self.name = Some(name.to_owned());
|
||||
ctx.text(format!("name changed to: {}", name));
|
||||
ctx.text(format!("name changed to: {name}"));
|
||||
} else {
|
||||
ctx.text("!!! name is required");
|
||||
}
|
||||
}
|
||||
|
||||
_ => ctx.text(format!("!!! unknown command: {:?}", msg)),
|
||||
_ => ctx.text(format!("!!! unknown command: {msg:?}")),
|
||||
}
|
||||
|
||||
return;
|
||||
|
@ -40,23 +40,23 @@ async fn main() {
|
||||
Some(msg) = framed.next() => {
|
||||
match msg {
|
||||
Ok(codec::ChatResponse::Message(ref msg)) => {
|
||||
println!("message: {}", msg);
|
||||
println!("message: {msg}");
|
||||
}
|
||||
Ok(codec::ChatResponse::Joined(ref msg)) => {
|
||||
println!("!!! joined: {}", msg);
|
||||
println!("!!! joined: {msg}");
|
||||
}
|
||||
|
||||
Ok(codec::ChatResponse::Rooms(rooms)) => {
|
||||
println!("!!! Available rooms:");
|
||||
for room in rooms {
|
||||
println!("{}", room);
|
||||
println!("{room}");
|
||||
}
|
||||
}
|
||||
|
||||
// respond to pings with a "pong"
|
||||
Ok(codec::ChatResponse::Ping) => { framed.send(codec::ChatRequest::Ping).await.unwrap(); },
|
||||
|
||||
_ => { eprintln!("{:?}", msg); }
|
||||
_ => { eprintln!("{msg:?}"); }
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -110,7 +110,7 @@ impl StreamHandler<Result<ws::Message, ws::ProtocolError>> for WsChatSession {
|
||||
Ok(msg) => msg,
|
||||
};
|
||||
|
||||
log::debug!("WEBSOCKET MESSAGE: {:?}", msg);
|
||||
log::debug!("WEBSOCKET MESSAGE: {msg:?}");
|
||||
match msg {
|
||||
ws::Message::Ping(msg) => {
|
||||
self.hb = Instant::now();
|
||||
@ -168,11 +168,11 @@ impl StreamHandler<Result<ws::Message, ws::ProtocolError>> for WsChatSession {
|
||||
ctx.text("!!! name is required");
|
||||
}
|
||||
}
|
||||
_ => ctx.text(format!("!!! unknown command: {:?}", m)),
|
||||
_ => ctx.text(format!("!!! unknown command: {m:?}")),
|
||||
}
|
||||
} else {
|
||||
let msg = if let Some(ref name) = self.name {
|
||||
format!("{}: {}", name, m)
|
||||
format!("{name}: {m}")
|
||||
} else {
|
||||
m.to_owned()
|
||||
};
|
||||
|
@ -102,7 +102,7 @@ impl StreamHandler<Result<ChatRequest, io::Error>> for ChatSession {
|
||||
// so actor wont receive any new messages until it get list of rooms back
|
||||
}
|
||||
Ok(ChatRequest::Join(name)) => {
|
||||
println!("Join to room: {}", name);
|
||||
println!("Join to room: {name}");
|
||||
self.room = name.clone();
|
||||
self.addr.do_send(server::Join {
|
||||
id: self.id,
|
||||
@ -112,7 +112,7 @@ impl StreamHandler<Result<ChatRequest, io::Error>> for ChatSession {
|
||||
}
|
||||
Ok(ChatRequest::Message(message)) => {
|
||||
// send message to chat server
|
||||
println!("Peer message: {}", message);
|
||||
println!("Peer message: {message}");
|
||||
self.addr.do_send(server::Message {
|
||||
id: self.id,
|
||||
msg: message,
|
||||
|
@ -42,7 +42,7 @@ async fn chat_route(
|
||||
/// Displays state
|
||||
async fn get_count(count: web::Data<AtomicUsize>) -> impl Responder {
|
||||
let current_count = count.load(Ordering::SeqCst);
|
||||
format!("Visitors: {}", current_count)
|
||||
format!("Visitors: {current_count}")
|
||||
}
|
||||
|
||||
#[actix_web::main]
|
||||
|
@ -135,7 +135,7 @@ impl Handler<Connect> for ChatServer {
|
||||
.insert(id);
|
||||
|
||||
let count = self.visitor_count.fetch_add(1, Ordering::SeqCst);
|
||||
self.send_message("Main", &format!("Total visitors {}", count), 0);
|
||||
self.send_message("Main", &format!("Total visitors {count}"), 0);
|
||||
|
||||
// send id back
|
||||
id
|
||||
|
@ -114,7 +114,7 @@ impl StreamHandler<Result<ws::Message, ws::ProtocolError>> for WsChatSession {
|
||||
Ok(msg) => msg,
|
||||
};
|
||||
|
||||
log::debug!("WEBSOCKET MESSAGE: {:?}", msg);
|
||||
log::debug!("WEBSOCKET MESSAGE: {msg:?}");
|
||||
match msg {
|
||||
ws::Message::Ping(msg) => {
|
||||
self.hb = Instant::now();
|
||||
@ -172,11 +172,11 @@ impl StreamHandler<Result<ws::Message, ws::ProtocolError>> for WsChatSession {
|
||||
ctx.text("!!! name is required");
|
||||
}
|
||||
}
|
||||
_ => ctx.text(format!("!!! unknown command: {:?}", m)),
|
||||
_ => ctx.text(format!("!!! unknown command: {m:?}")),
|
||||
}
|
||||
} else {
|
||||
let msg = if let Some(ref name) = self.name {
|
||||
format!("{}: {}", name, m)
|
||||
format!("{name}: {m}")
|
||||
} else {
|
||||
m.to_owned()
|
||||
};
|
||||
|
@ -44,7 +44,7 @@ async fn main() {
|
||||
match msg {
|
||||
Ok(ws::Frame::Text(txt)) => {
|
||||
// log echoed messages from server
|
||||
log::info!("Server: {:?}", txt)
|
||||
log::info!("Server: {txt:?}")
|
||||
}
|
||||
|
||||
Ok(ws::Frame::Ping(_)) => {
|
||||
|
@ -57,7 +57,7 @@ impl Actor for MyWebSocket {
|
||||
impl StreamHandler<Result<ws::Message, ws::ProtocolError>> for MyWebSocket {
|
||||
fn handle(&mut self, msg: Result<ws::Message, ws::ProtocolError>, ctx: &mut Self::Context) {
|
||||
// process websocket messages
|
||||
println!("WS: {:?}", msg);
|
||||
println!("WS: {msg:?}");
|
||||
match msg {
|
||||
Ok(ws::Message::Ping(msg)) => {
|
||||
self.hb = Instant::now();
|
||||
|
Reference in New Issue
Block a user