Connections are properly closed

This commit is contained in:
Pierre HUBERT 2022-10-16 18:35:07 +02:00
parent 70d70c2851
commit 1c08e2ec01
3 changed files with 14 additions and 2 deletions

View File

@ -162,4 +162,11 @@ impl Client {
pub async fn recv_next_message(&self) -> Res<ServerMessage> { pub async fn recv_next_message(&self) -> Res<ServerMessage> {
Ok(self.receiver.recv()?) Ok(self.receiver.recv()?)
} }
/// Close connection
pub async fn close_connection(&mut self) {
if let Err(e) = self.sink.send(Message::Close(None)).await {
log::debug!("Failed to close WS connection! {:?}", e);
}
}
} }

View File

@ -145,6 +145,7 @@ impl GameScreen {
KeyCode::Char('q') KeyCode::Char('q')
if confirm(terminal, "Do you really want to leave game?") => if confirm(terminal, "Do you really want to leave game?") =>
{ {
self.client.close_connection().await;
return Ok(ScreenResult::Canceled); return Ok(ScreenResult::Canceled);
} }
@ -192,7 +193,10 @@ impl GameScreen {
.await?; .await?;
self.status = GameStatus::RematchRejected; self.status = GameStatus::RematchRejected;
} }
Buttons::QuitGame => return Ok(ScreenResult::Ok(())), Buttons::QuitGame => {
self.client.close_connection().await;
return Ok(ScreenResult::Ok(()));
}
}, },
_ => {} _ => {}
@ -249,6 +253,7 @@ impl GameScreen {
.await? .await?
} }
ScreenResult::Canceled => { ScreenResult::Canceled => {
self.client.close_connection().await;
return Ok(ScreenResult::Canceled); return Ok(ScreenResult::Canceled);
} }
}; };

View File

@ -72,7 +72,7 @@ impl GameStatus {
pub fn can_game_continue_with_bot(&self) -> bool { pub fn can_game_continue_with_bot(&self) -> bool {
*self != GameStatus::Finished *self != GameStatus::Finished
&& *self != GameStatus::RematchRejected && *self != GameStatus::RematchRejected
&& *self != GameStatus::RematchRejected && *self != GameStatus::RematchRequested
} }
} }