Skip to content

Commit

Permalink
fix: chat-app do not send empty messages
Browse files Browse the repository at this point in the history
  • Loading branch information
threadexio committed Dec 17, 2023
1 parent ef5dfff commit 6a84f88
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
2 changes: 1 addition & 1 deletion examples/chat-app/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub enum ServerNetMessage {
UserConnected { name: String },
}

type Serdes = channels::serdes::Json;
type Serdes = channels::serdes::Bincode;

#[derive(Debug)]
pub struct ClientSide {
Expand Down
13 changes: 8 additions & 5 deletions examples/chat-app/src/connect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,15 @@ pub async fn connect(
line.clear();
stdin.read_line(&mut line).await.context("failed to read line from stdin")
} => {
r?;
let _ = r?;
let line = line.trim();

state
.net
.send(ClientNetMessage::SendMessage { content: line.trim().to_owned() })
.await.context("failed to send message")?;
if !line.is_empty() {
state
.net
.send(ClientNetMessage::SendMessage { content: line.to_owned() })
.await.context("failed to send message")?;
}
}
r = state.net.recv() => {
let message = r.context("failed to receive message")?;
Expand Down
11 changes: 7 additions & 4 deletions examples/chat-app/src/serve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,13 @@ async fn handle_client(state: State<Unauthenticated>) -> Result<()> {
r = client.net.recv() => {
match r? {
ClientNetMessage::SendMessage { content } => {
client.srv.send(Notification::ChatMessage {
owner: client.state.name.clone(),
content,
});
let content = content.trim();
if !content.is_empty() {
client.srv.send(Notification::ChatMessage {
owner: client.state.name.clone(),
content: content.to_owned(),
});
}
}
_ => bail!("unexpected message received")
}
Expand Down

0 comments on commit 6a84f88

Please sign in to comment.