get room uuid command

This commit is contained in:
Víctor Martínez 2021-03-22 00:19:33 +01:00
parent cad0039166
commit 64c80bf54a
3 changed files with 25 additions and 7 deletions

View file

@ -102,4 +102,5 @@ If an error occurs, the server will send back a json with the following format:
## Todo List ## Todo List
+ Add a redis db to store messages & active rooms + Add a redis db to store messages & active rooms
+ Add TLS certificate

View file

@ -200,8 +200,7 @@ impl WsChatSession {
fn msg(&self, msg: String, ctx: &mut WebsocketContext<Self>) { fn msg(&self, msg: String, ctx: &mut WebsocketContext<Self>) {
match Command::from_str(&msg) { match Command::from_str(&msg) {
Ok(cmd) if self.room.is_some() => ctx.notify(cmd), Ok(cmd) => ctx.notify(cmd),
Ok(_) => ctx.text(WsMessage::err("You are not in a room yet".into())),
Err(err) => ctx.text(WsMessage::err(err.to_string())), Err(err) => ctx.text(WsMessage::err(err.to_string())),
} }
} }
@ -233,15 +232,23 @@ impl WsChatSession {
impl Handler<Command> for WsChatSession { impl Handler<Command> for WsChatSession {
type Result = (); 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 { match msg {
Command::Msg(msg) => { Command::Msg(msg) => {
self.addr.do_send(ClientMessage { self.addr.do_send(ClientMessage {
session: self.id.clone(), session: self.id.clone(),
room: self.room.clone().unwrap(), room: room_id,
msg, msg,
}); });
} }
Command::GetRoomId => {
ctx.text(WsMessage::info(room_id.to_string()));
}
} }
} }
} }

View file

@ -7,6 +7,7 @@ use std::str::FromStr;
#[rtype(result = "()")] #[rtype(result = "()")]
pub enum Command { pub enum Command {
Msg(String), Msg(String),
GetRoomId,
} }
#[derive(Debug, Display, Error)] #[derive(Debug, Display, Error)]
@ -19,7 +20,16 @@ pub struct CommandError {
impl FromStr for Command { impl FromStr for Command {
type Err = CommandError; type Err = CommandError;
fn from_str(s: &str) -> Result<Self, Self::Err> { fn from_str(data: &str) -> Result<Self, Self::Err> {
Ok(Command::Msg(s.into())) 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()))
} }
} }