Skip to content

Commit

Permalink
temp client
Browse files Browse the repository at this point in the history
  • Loading branch information
SaHHiiLL committed Nov 23, 2023
1 parent 0b9fdfe commit c3d13f4
Showing 1 changed file with 22 additions and 8 deletions.
30 changes: 22 additions & 8 deletions client/src/main.rs
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}");
}
}

0 comments on commit c3d13f4

Please sign in to comment.