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