Handle ping/pong messages for Ws provider (#163)

This commit is contained in:
Sam Wilson 2021-01-15 04:33:38 -05:00 committed by GitHub
parent fc4b3e4ce4
commit 8eafe81aa9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 13 additions and 5 deletions

View File

@ -251,12 +251,20 @@ where
} }
async fn handle_ws(&mut self, resp: Message) -> Result<(), ClientError> { async fn handle_ws(&mut self, resp: Message) -> Result<(), ClientError> {
// Get the inner text received from the websocket match resp {
let inner = match resp { Message::Text(inner) => self.handle_text(inner).await,
Message::Text(inner) => inner, Message::Ping(inner) => self.handle_ping(inner).await,
_ => return Err(ClientError::NoResponse), Message::Pong(_) => Ok(()), // Server is allowed to send unsolicited pongs.
}; _ => Err(ClientError::NoResponse),
}
}
async fn handle_ping(&mut self, inner: Vec<u8>) -> Result<(), ClientError> {
self.ws.send(Message::Pong(inner)).await?;
Ok(())
}
async fn handle_text(&mut self, inner: String) -> Result<(), ClientError> {
if let Ok(resp) = serde_json::from_str::<Response<serde_json::Value>>(&inner) { if let Ok(resp) = serde_json::from_str::<Response<serde_json::Value>>(&inner) {
if let Some(request) = self.pending.remove(&resp.id) { if let Some(request) = self.pending.remove(&resp.id) {
request request