mirror of
https://github.com/rjNemo/tokio-chat-server
synced 2026-06-06 01:26:40 +00:00
remove echo to sender
This commit is contained in:
parent
80cb04fb71
commit
f4decd8eee
1 changed files with 7 additions and 5 deletions
12
src/main.rs
12
src/main.rs
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue