diff options
| author | Fuwn <[email protected]> | 2021-03-21 11:38:24 -0700 |
|---|---|---|
| committer | GitHub <[email protected]> | 2021-03-21 11:38:24 -0700 |
| commit | 9e32aafdbde674e4ef7845f57a30111367a46864 (patch) | |
| tree | 2962d4fc2a5064111b7279adf8cc435177792b7a /src | |
| parent | chore: create broadcast_to_all_clients() function, rename sub directory (diff) | |
| parent | etc: development testing (diff) | |
| download | whirl-9e32aafdbde674e4ef7845f57a30111367a46864.tar.xz whirl-9e32aafdbde674e4ef7845f57a30111367a46864.zip | |
Merge pull request #1 from Vencorr/master
Add a username to Text
Diffstat (limited to 'src')
| -rw-r--r-- | src/cmd/text.rs | 13 | ||||
| -rw-r--r-- | src/server/world.rs | 16 |
2 files changed, 20 insertions, 9 deletions
diff --git a/src/cmd/text.rs b/src/cmd/text.rs index b2cecef..6fc42be 100644 --- a/src/cmd/text.rs +++ b/src/cmd/text.rs @@ -1,11 +1,12 @@ -pub fn create_text_command(message: &str) -> Vec<u8> { - let mut text = Vec::with_capacity(6 + message.len()); +pub fn create_text_command(user: &str, message: &str) -> Vec<u8> { + let mut text = Vec::with_capacity(6 + user.len() + message.len()); text.push(0x01); // ? text.push(0x0E); // Command type text.push(0x00); // Assumed to be a divider. - text.push(0x00); // ^ - text.push(message.len() as u8); // `message` length - for i in message.bytes() { text.push(i); } // `message` + text.push(user.len() as u8); // 'user' length + for i in user.bytes() { text.push(i); } // Pushing 'user' + text.push(message.len() as u8); // 'message' length + for i in message.bytes() { text.push(i); } // Pushing `message` text.insert(0, text.len() as u8 + 1); // Insert data length as first byte. text // Return created array @@ -16,4 +17,4 @@ pub fn create_text_command(message: &str) -> Vec<u8> { // from_utf8( // &buffer[6..*&buffer.get(0).unwrap().to_owned() as usize] // ).unwrap() -// } +// }
\ No newline at end of file diff --git a/src/server/world.rs b/src/server/world.rs index ddefd39..340942f 100644 --- a/src/server/world.rs +++ b/src/server/world.rs @@ -6,6 +6,7 @@ use std::str::from_utf8; use crate::cmd::buddy_list::create_buddy_list_notify_command; use crate::cmd::text::create_text_command; use crate::cmd::property::{create_property_update_command, create_property_request_command}; +use rand::Rng; // pub struct ClientSocket { // tcp_stream: TcpStream, @@ -123,9 +124,18 @@ impl WorldServer { ).unwrap(); info!("message: {}", message); - for mut socket in &sockets { - socket.1.write_all(&create_text_command(message)).unwrap(); - } + // Using User as a placeholder. Ideally, this would print out the username of + // the one who sent it. + broadcast_to_all_clients( + &sockets, + &create_text_command( + // Random integer is added to the end of "User", just a development + // proof-of-concept. Since at this stage usernames aren't exactly kept, + // we can identify message senders as their connection token; `token.0`. + &format!("User{}", rand::thread_rng().gen_range(1..150).to_string()), + message + ) + ); } // SESSEXIT 7 => { |