33 lines
859 B
Rust
33 lines
859 B
Rust
use tokio::{io::AsyncWriteExt, net::TcpStream};
|
|
|
|
use crate::{message::Message, player::Player};
|
|
|
|
pub struct Client {
|
|
pub server_host: String,
|
|
pub server_port: u16,
|
|
pub stream: TcpStream,
|
|
}
|
|
|
|
impl Client {
|
|
pub async fn connect(host: impl Into<String>, port: u16) -> anyhow::Result<Self> {
|
|
let host = host.into();
|
|
let address = format!("{}:{}", host, port);
|
|
let stream = TcpStream::connect(address).await?;
|
|
|
|
Ok(Self {
|
|
server_host: host,
|
|
server_port: port,
|
|
stream,
|
|
})
|
|
}
|
|
|
|
pub async fn send_message(&mut self, message: Message) -> anyhow::Result<()> {
|
|
let message_string = message.encode()?;
|
|
self.stream.write_all(&message_string.as_bytes()).await?;
|
|
Ok(())
|
|
}
|
|
|
|
pub async fn create_player() -> Option<Player> {
|
|
todo!()
|
|
}
|
|
}
|