1
0
mirror of https://github.com/actix/examples synced 2025-06-27 09:29:02 +02:00

remove select macro

This commit is contained in:
Rob Ede
2022-07-12 01:42:58 +01:00
parent 0b93208bb2
commit 25368e6b65
5 changed files with 97 additions and 41 deletions

View File

@ -2,6 +2,7 @@
use std::{
collections::{HashMap, HashSet},
io,
sync::{
atomic::{AtomicUsize, Ordering},
Arc,
@ -62,7 +63,8 @@ impl ChatServer {
for conn_id in sessions {
if *conn_id != skip_id {
if let Some(tx) = self.sessions.get(conn_id) {
tx.send(msg.clone()).unwrap();
// errors if client disconnected abruptly and hasn't been timed-out yet
let _ = tx.send(msg.clone());
}
}
}
@ -152,12 +154,17 @@ impl ChatServer {
self.send_message(&room, "Someone connected", conn_id).await;
}
pub async fn run(mut self) {
pub async fn run(mut self) -> io::Result<()> {
loop {
match self.rx.recv().await.unwrap() {
let cmd = match self.rx.recv().await {
Some(cmd) => cmd,
None => break,
};
match cmd {
Command::Connect { conn_tx, res_tx } => {
let conn_id = self.connect(conn_tx).await;
res_tx.send(conn_id).unwrap();
let _ = res_tx.send(conn_id);
}
Command::Disconnect { conn } => {
@ -165,12 +172,12 @@ impl ChatServer {
}
Command::List { res_tx } => {
res_tx.send(self.list_rooms()).unwrap();
let _ = res_tx.send(self.list_rooms());
}
Command::Join { conn, room, res_tx } => {
self.join_room(conn, room).await;
res_tx.send(()).unwrap();
let _ = res_tx.send(());
}
Command::Message {
@ -180,9 +187,11 @@ impl ChatServer {
res_tx,
} => {
self.send_message(&room, msg, skip).await;
res_tx.send(()).unwrap();
let _ = res_tx.send(());
}
}
}
Ok(())
}
}