4 releases (2 breaking)
new 0.3.0 | Mar 7, 2025 |
---|---|
0.2.0 | Mar 7, 2025 |
0.1.1 | Mar 6, 2025 |
0.1.0 | Mar 6, 2025 |
#1834 in Network programming
85 downloads per month
Used in minechat-client
14KB
139 lines
MineChat protocol
MineChat is a Rust library designed to facilitate communication with a Minecraft chat server. It provides asynchronous functions to send and receive messages, handle authentication, and manage connections.
Features
- Asynchronous message sending and receiving.
- Error handling with detailed error messages.
- UUID generation for client identification.
- Support for various message types (Auth, AuthAck, Chat, Broadcast, Disconnect).
- Integration with
tokio
for asynchronous I/O operations. - Logging for debugging and monitoring.
Usage
Sending a Message
To send a message to the server, use the send_message
function:
use minechat_protocol::{send_message, MineChatMessage};
use tokio::net::TcpStream;
#[tokio::main]
async fn main() {
let server_addr = "127.0.0.1:8080";
let mut stream = TcpStream::connect(server_addr).await.unwrap();
let (mut reader, mut writer) = stream.split();
let message = MineChatMessage::Chat {
payload: ChatPayload {
message: "Hello, server!".to_string(),
},
};
if let Err(e) = send_message(&mut writer, &message).await {
eprintln!("Failed to send message: {}", e);
}
}
Receiving a Message
To receive a message from the server, use the receive_message
function:
use minechat_protocol::receive_message;
use tokio::net::TcpStream;
use tokio::io::BufReader;
#[tokio::main]
async fn main() {
let server_addr = "127.0.0.1:8080";
let mut stream = TcpStream::connect(server_addr).await.unwrap();
let (reader, _) = stream.split();
let mut reader = BufReader::new(reader);
match receive_message(&mut reader).await {
Ok(message) => println!("Received message: {:?}", message),
Err(e) => eprintln!("Failed to receive message: {}", e),
}
}
Handling Linking
To handle linking with the server, use the handle_link
function:
use minechat_protocol::handle_link;
#[tokio::main]
async fn main() {
let server_addr = "127.0.0.1:8080";
let link_code = "your_link_code";
match handle_link(server_addr, link_code).await {
Ok((server, client_uuid)) => {
println!("Linked successfully to server {} with client UUID {}", server, client_uuid);
}
Err(e) => eprintln!("Failed to link: {}", e),
}
}
License
This project is licensed under the MPL-2.0 License. See the LICENSE file for more details.
Contributing
Contributions are welcome! Please open an issue or submit a pull request.
Roadmap
- Expose a synchronous API.
Contact
For any questions or support, please open an issue on the GitHub repository.
Dependencies
~5–14MB
~160K SLoC