Handle errors properly when WebSocket client disconnects improperly

Closes #181
Closes #195
Closes #140
This commit is contained in:
Tobie Morgan Hitchcock 2022-09-20 23:04:40 +01:00
parent b06da47da6
commit 1dd08bedaa

View file

@ -3,6 +3,7 @@ use crate::cnf::MAX_CONCURRENT_CALLS;
use crate::dbs::DB;
use crate::err::Error;
use crate::net::session;
use crate::net::LOG;
use crate::rpc::args::Take;
use crate::rpc::paths::{ID, METHOD, PARAMS};
use crate::rpc::res::Failure;
@ -60,17 +61,36 @@ impl Rpc {
let (mut wtx, mut wrx) = ws.split();
// Send messages to the client
tokio::task::spawn(async move {
// Wait for the next message to send
while let Some(res) = rcv.next().await {
let _ = wtx.send(res).await;
// Send the message to the client
if let Err(err) = wtx.send(res).await {
// Output the WebSocket error to the logs
trace!(target: LOG, "WebSocket error: {:?}", err);
// It's already failed, so ignore error
let _ = wtx.close().await;
// Exit out of the loop
break;
}
}
});
// Get messages from the client
while let Some(msg) = wrx.next().await {
if let Ok(msg) = msg {
match msg {
// We've received a message from the client
Ok(msg) => {
if msg.is_text() {
tokio::task::spawn(Rpc::call(rpc.clone(), msg, chn.clone()));
}
}
// There was an error receiving the message
Err(err) => {
// Output the WebSocket error to the logs
trace!(target: LOG, "WebSocket error: {:?}", err);
// Exit out of the loop
break;
}
}
}
}