remove echo to sender

This commit is contained in:
Ruidy 2021-09-01 15:00:47 +02:00
parent 80cb04fb71
commit f4decd8eee

View file

@ -5,10 +5,10 @@ use tokio::sync::broadcast;
#[tokio::main]
async fn main() {
let listener = TcpListener::bind("localhost:8080").await.unwrap();
let (tx, _) = broadcast::channel::<String>(10);
let (tx, _) = broadcast::channel(10);
loop {
let (mut socket, _) = listener.accept().await.unwrap();
let (mut socket, addr) = listener.accept().await.unwrap();
let tx = tx.clone();
let mut rx = tx.subscribe();
@ -25,12 +25,14 @@ async fn main() {
if result.unwrap() == 0 {
break;
}
tx.send(line.clone()).unwrap();
tx.send((line.clone(),addr)).unwrap();
line.clear();
}
result = rx.recv() =>{
let message = result.unwrap();
writer.write_all(&message.as_bytes()).await.unwrap();
let (message,other_addr) = result.unwrap();
if addr != other_addr{
writer.write_all(&message.as_bytes()).await.unwrap();
}
}
}
}