mirror of
https://github.com/rjNemo/tokio-chat-server
synced 2026-06-12 12:16:38 +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]
|
#[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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue