diff --git a/client/src/components/chat.rs b/client/src/components/chat.rs index e640449..fdab86f 100644 --- a/client/src/components/chat.rs +++ b/client/src/components/chat.rs @@ -21,16 +21,19 @@ pub fn Chat() -> impl IntoView { create_effect(move |_| { websocket.ready_state.with(move |state| match *state { ConnectionReadyState::Connecting => { - update_chat_history(&set_chat_history, format!("Connecting to game server...\n")); + update_chat_history( + &set_chat_history, + "Connecting to game server...\n".to_string(), + ); } ConnectionReadyState::Open => { - update_chat_history(&set_chat_history, format!("Connected!\n")); + update_chat_history(&set_chat_history, "Connected!\n".to_string()); } ConnectionReadyState::Closing => { - update_chat_history(&set_chat_history, format!("Disconnecting...\n")); + update_chat_history(&set_chat_history, "Disconnecting...\n".to_string()); } ConnectionReadyState::Closed => { - update_chat_history(&set_chat_history, format!("Disconnected.\n")); + update_chat_history(&set_chat_history, "Disconnected.\n".to_string()); } }) }); @@ -48,7 +51,9 @@ pub fn Chat() -> impl IntoView { if let Some(message) = message_raw { if let Ok(_game) = serde_json::from_str::(message) { logging::log!("Game object received at chat component"); - } else if let Ok(_) = serde_json::from_str::(message) { + } else if let Ok(_state_summary) = + serde_json::from_str::(message) + { logging::log!("State Summary received at chat component"); } else { update_chat_history(&set_chat_history, format!("{}\n", message)); @@ -58,7 +63,9 @@ pub fn Chat() -> impl IntoView { }); let send_message = move |_| { - websocket.send(&chat_input_ref.get().unwrap().value()); + websocket.send(&serde_json::to_string(&ChatMessage { + text: chat_input_ref.get().unwrap().value(), + }).unwrap()); chat_input_ref.get().unwrap().set_value(""); logging::log!("Send Message"); }; diff --git a/lib/src/models.rs b/lib/src/models.rs index 2e1cef5..748b546 100644 --- a/lib/src/models.rs +++ b/lib/src/models.rs @@ -1,6 +1,12 @@ use serde::{Deserialize, Serialize}; use std::net::SocketAddr; +/// Chat message +#[derive(Serialize, Deserialize, Debug)] +pub struct ChatMessage { + pub text: String, +} + /// Server state summary #[derive(Serialize, Deserialize, Debug)] pub struct ServerStateSummary { diff --git a/server/src/api/message_handler.rs b/server/src/api/message_handler.rs index d268083..fd7f015 100644 --- a/server/src/api/message_handler.rs +++ b/server/src/api/message_handler.rs @@ -24,11 +24,12 @@ pub async fn message_handler(message: Message, state: &Arc, who: &User } else { let _res = tx.send(String::from("error creating game")); } - } else { - // just echo - let msg = format! {"{0}: {1}", who.name, text}; + } else if let Ok(chat_message) = serde_json::from_str::(&text) { + let msg = format! {"{0}: {1}", who.name, chat_message.text}; tracing::debug!("{msg}"); let _res = tx.send(msg); + } else { + tracing::debug!("Unhandled message: {}", &text); } }