-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
22 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,31 @@ | ||
use std::io::Read; | ||
use std::io::Write; | ||
use std::net::TcpStream; | ||
use std::sync::Arc; | ||
|
||
use chat_messages::Message; | ||
use termion::color; | ||
use termion::clear; | ||
|
||
fn main() { | ||
let address = "127.0.0.1:6969"; | ||
let mut stream = TcpStream::connect(address).unwrap(); | ||
let msg = String::from("Hello from client"); | ||
let msg = Message::new(msg); | ||
let msg = serde_json::to_string(&msg).unwrap(); | ||
let msg = msg.trim(); | ||
|
||
println!("{msg}"); | ||
println!("{}", clear::All); | ||
|
||
let address = "127.0.0.1:6969"; | ||
let mut stream = Arc::new(TcpStream::connect(address).unwrap()); | ||
|
||
writeln!(stream, "{msg}"); | ||
loop { | ||
let mut buff = [0; 10000]; | ||
let x = stream.as_ref().read(&mut buff).unwrap(); | ||
let vec = buff[0..x].to_vec(); | ||
let from_utf8 = String::from_utf8(vec).unwrap(); | ||
println!("{from_utf8}"); | ||
let x = serde_json::from_str::<Message>(&from_utf8).unwrap(); | ||
// let msg = if x.name().is_some() { | ||
let msg = format!("{}{}{}: {}", color::Red.fg_str(), &x.name().clone(), color::Reset.fg_str(), x.message()); | ||
// } else { | ||
// format!("{}{}{}: {}", color::Red.fg_str(), stream.local_addr().unwrap().to_string(), color::Reset.fg_str(), x.message()) | ||
// }; | ||
println!("{msg}"); | ||
} | ||
} |