Skip to content

Commit

Permalink
feat: wip say
Browse files Browse the repository at this point in the history
  • Loading branch information
jcs090218 committed Jun 13, 2024
1 parent 071f376 commit 0b7076d
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
17 changes: 15 additions & 2 deletions src/handler/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,11 +229,24 @@ pub mod say {
return;
}

let username = client.user().unwrap().username.clone();

let file = data_str(json, "file").unwrap();
let file = no_room_path(&room, &file);
let message = data_str(json, "message").unwrap();

room.broadcast_json(&serde_json::json!({
let peers = room.peers_by_file(&room, &file);

let params = &serde_json::json!({
"method": METHOD,
"username": username, // Who speak this message?
"file": file,
"message": message,
}));
"status": "success",
});

for (_addr, _sender) in peers.iter() {
let _ = _sender.send(params.to_string());
}
}
}
22 changes: 22 additions & 0 deletions src/room.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,28 @@ impl Room {
&mut self.peers
}

/// Return peers by file.
///
/// # Arguments
///
/// * `file` - The file path.
pub fn peers_by_file(&self, room: &Room, file: &String) -> HashMap<&SocketAddr, &UnboundedSender<String>> {
let file = no_room_path(&room, &file);

let mut data = HashMap::new();

for (addr, sender) in self.peers.iter() {
let client = self.get_client(addr).unwrap();
let path = no_room_path(&room, client.get_path());
if path == file {
continue;
}
data.insert(addr, sender);
}

data
}

/// Return the sender.
///
/// # Arguments
Expand Down

0 comments on commit 0b7076d

Please sign in to comment.