Properly detect client disconnect

This commit is contained in:
Yash Karandikar 2021-12-07 17:51:04 -06:00
parent 28b85b1d6c
commit 3e8e9444a5
Signed by: karx
GPG key ID: A794DA2529474BA5

View file

@ -28,15 +28,13 @@ async fn handle_connection(
println!("IP Address: {}", a);
clients.lock().await.insert(a, 0);
loop {
let (sockread, mut sockwrite) = socket.split();
if sockwrite.write_all(b"PING\n").await.is_err() {
break;
};
let mut buf = [0u8; 1024];
let bytes = sockread.take(1024u64).read(&mut buf).await.unwrap();
let bytes = match socket.read(&mut buf).await {
Ok(0) => break, // eof
Ok(n) => n,
Err(e) => panic!("error occurred: {}", e)
};
print!("{}", String::from_utf8_lossy(&buf[..bytes]).to_string());
}
println!("Client disconnected");