Handle errors properly when WebSocket client disconnects improperly
Closes #181 Closes #195 Closes #140
This commit is contained in:
parent
b06da47da6
commit
1dd08bedaa
1 changed files with 24 additions and 4 deletions
|
@ -3,6 +3,7 @@ use crate::cnf::MAX_CONCURRENT_CALLS;
|
||||||
use crate::dbs::DB;
|
use crate::dbs::DB;
|
||||||
use crate::err::Error;
|
use crate::err::Error;
|
||||||
use crate::net::session;
|
use crate::net::session;
|
||||||
|
use crate::net::LOG;
|
||||||
use crate::rpc::args::Take;
|
use crate::rpc::args::Take;
|
||||||
use crate::rpc::paths::{ID, METHOD, PARAMS};
|
use crate::rpc::paths::{ID, METHOD, PARAMS};
|
||||||
use crate::rpc::res::Failure;
|
use crate::rpc::res::Failure;
|
||||||
|
@ -60,17 +61,36 @@ impl Rpc {
|
||||||
let (mut wtx, mut wrx) = ws.split();
|
let (mut wtx, mut wrx) = ws.split();
|
||||||
// Send messages to the client
|
// Send messages to the client
|
||||||
tokio::task::spawn(async move {
|
tokio::task::spawn(async move {
|
||||||
|
// Wait for the next message to send
|
||||||
while let Some(res) = rcv.next().await {
|
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
|
// Get messages from the client
|
||||||
while let Some(msg) = wrx.next().await {
|
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() {
|
if msg.is_text() {
|
||||||
tokio::task::spawn(Rpc::call(rpc.clone(), msg, chn.clone()));
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue