diff --git a/README.md b/README.md index 27ae5a6..37ab288 100644 --- a/README.md +++ b/README.md @@ -102,4 +102,5 @@ If an error occurs, the server will send back a json with the following format: ## Todo List -+ Add a redis db to store messages & active rooms \ No newline at end of file ++ Add a redis db to store messages & active rooms ++ Add TLS certificate \ No newline at end of file diff --git a/src/actors/chat_session.rs b/src/actors/chat_session.rs index a36511a..7bc2611 100644 --- a/src/actors/chat_session.rs +++ b/src/actors/chat_session.rs @@ -200,8 +200,7 @@ impl WsChatSession { fn msg(&self, msg: String, ctx: &mut WebsocketContext) { match Command::from_str(&msg) { - Ok(cmd) if self.room.is_some() => ctx.notify(cmd), - Ok(_) => ctx.text(WsMessage::err("You are not in a room yet".into())), + Ok(cmd) => ctx.notify(cmd), Err(err) => ctx.text(WsMessage::err(err.to_string())), } } @@ -233,15 +232,23 @@ impl WsChatSession { impl Handler for WsChatSession { type Result = (); - fn handle(&mut self, msg: Command, _ctx: &mut Self::Context) -> Self::Result { + fn handle(&mut self, msg: Command, ctx: &mut Self::Context) -> Self::Result { + if let None = self.room { + ctx.text(WsMessage::err("You are not in a room".into())); + return; + } + let room_id = self.room.clone().unwrap(); match msg { Command::Msg(msg) => { self.addr.do_send(ClientMessage { session: self.id.clone(), - room: self.room.clone().unwrap(), + room: room_id, msg, }); } + Command::GetRoomId => { + ctx.text(WsMessage::info(room_id.to_string())); + } } } } diff --git a/src/messages/session/command.rs b/src/messages/session/command.rs index a352f79..19fc2ea 100644 --- a/src/messages/session/command.rs +++ b/src/messages/session/command.rs @@ -7,6 +7,7 @@ use std::str::FromStr; #[rtype(result = "()")] pub enum Command { Msg(String), + GetRoomId, } #[derive(Debug, Display, Error)] @@ -19,7 +20,16 @@ pub struct CommandError { impl FromStr for Command { type Err = CommandError; - fn from_str(s: &str) -> Result { - Ok(Command::Msg(s.into())) + fn from_str(data: &str) -> Result { + let words: Vec<&str> = data.trim().split_whitespace().collect(); + let first_word = words.first(); + + if let Some(&word) = first_word { + return match word { + "/roomId" => Ok(Command::GetRoomId), + _ => Ok(Command::Msg(data.into())), + }; + } + Ok(Command::Msg(data.into())) } }