Skip to content

Commit c87efed

Browse files
mikemiles-devmikemiles-dev
authored andcommitted
fix: Better client reconnection
1 parent 0f3481f commit c87efed

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

RELEASES.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# 0.1.6
2+
* Better client reconnection.
3+
14
# 0.1.5
25
* More robust dead connection logic.
36
* Fixed issues with input cursor and ctrl + c

client/src/client.rs

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -651,12 +651,42 @@ impl ChatClient {
651651
Ok(input::ClientUserInput::ListUsers) => {
652652
let message = ChatMessage::try_new(MessageTypes::ListUsers, None)
653653
.map_err(|e| io::Error::other(format!("Failed to create ListUsers message: {e:?}")))?;
654-
self.send_message_chunked(message).await
655-
.map_err(|e| io::Error::other(format!("Failed to send ListUsers message: {e:?}")))?;
654+
if let Err(e) = self.send_message_chunked(message).await {
655+
logger::log_warning("Connection lost while sending message");
656+
657+
if !self.was_kicked {
658+
match self.reconnect().await {
659+
Ok(()) => {
660+
// Connection restored
661+
}
662+
Err(reconnect_err) => {
663+
logger::log_error(&format!("Failed to reconnect: {:?}", reconnect_err));
664+
return Err(io::Error::other(format!("Failed to send ListUsers message: {e:?}")));
665+
}
666+
}
667+
}
668+
}
656669
}
657670
Ok(user_input) => {
658671
if let Err(e) = self.handle_user_input(user_input).await {
659-
logger::log_error(&format!("Error: {e:?}"));
672+
// Check if this is a connection error that needs reconnection
673+
if matches!(e, ChatClientError::IoError) {
674+
logger::log_warning("Connection lost while sending message");
675+
676+
if !self.was_kicked {
677+
match self.reconnect().await {
678+
Ok(()) => {
679+
// Connection restored
680+
}
681+
Err(reconnect_err) => {
682+
logger::log_error(&format!("Failed to reconnect: {:?}", reconnect_err));
683+
return Err(io::Error::other("Reconnection failed"));
684+
}
685+
}
686+
}
687+
} else {
688+
logger::log_error(&format!("Error: {e:?}"));
689+
}
660690
}
661691
}
662692
Err(e) => {

0 commit comments

Comments
 (0)