start of client app
This commit is contained in:
parent
6714735633
commit
52b6422e1a
9
Cargo.lock
generated
9
Cargo.lock
generated
@ -2,6 +2,15 @@
|
|||||||
# It is not intended for manual editing.
|
# It is not intended for manual editing.
|
||||||
version = 3
|
version = 3
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "prompted"
|
||||||
|
version = "0.2.8"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "d34cf5bf48e2fe6e11fb6b619fb5962530f1eaf278709de5f8ea49afa7379f9b"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "rust-irc"
|
name = "rust-irc"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"prompted",
|
||||||
|
]
|
||||||
|
@ -6,3 +6,4 @@ edition = "2021"
|
|||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
prompted = "0.2.8"
|
||||||
|
@ -1,4 +1,64 @@
|
|||||||
|
use prompted::input;
|
||||||
|
use std::io::{self, Read, Write};
|
||||||
|
use std::net::TcpStream;
|
||||||
|
use std::thread;
|
||||||
|
|
||||||
|
fn read_messages(mut stream: TcpStream) {
|
||||||
|
let mut buffer: [u8; 1024] = [0; 1024];
|
||||||
|
loop {
|
||||||
|
match stream.read(&mut buffer) {
|
||||||
|
Ok(size) => {
|
||||||
|
if size == 0 {
|
||||||
|
break; //Server closed connection
|
||||||
|
}
|
||||||
|
let message: &[u8] = &buffer[..size];
|
||||||
|
process_message(message);
|
||||||
|
}
|
||||||
|
Err(_) => {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn process_message(message: &[u8]) {
|
||||||
|
if let Ok(text) = String::from_utf8(message.to_vec()) {
|
||||||
|
println!("{}", text);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn start() {
|
pub fn start() {
|
||||||
println!("Starting the IRC client");
|
println!("Starting the IRC client");
|
||||||
todo!();
|
// let nick: String = input!("Enter your nickname: ");
|
||||||
|
let nick: String = "testy".to_string();
|
||||||
|
// let host: String = input!("Enter the server host: ");
|
||||||
|
let host: &str = "localhost";
|
||||||
|
|
||||||
|
if let Ok(mut stream) = TcpStream::connect(host.to_owned() + ":6667") {
|
||||||
|
println!("Connected to {}", host);
|
||||||
|
|
||||||
|
let stream_clone: TcpStream = stream.try_clone().expect("Faile to clone stream");
|
||||||
|
thread::spawn(move || {
|
||||||
|
read_messages(stream_clone);
|
||||||
|
});
|
||||||
|
|
||||||
|
loop {
|
||||||
|
let cmd: String = input!(":");
|
||||||
|
match cmd.trim() {
|
||||||
|
"/quit" => {}
|
||||||
|
"/list" => {}
|
||||||
|
"/msq" => {}
|
||||||
|
"/join" => {}
|
||||||
|
"/show" => {}
|
||||||
|
"/leave" => {}
|
||||||
|
"/msg" => {}
|
||||||
|
|
||||||
|
_ => {
|
||||||
|
stream.write(cmd.as_bytes());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
println!("Failed to connect to {} with nickname {} ", host, nick);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user