Crates.io | sockit |
lib.rs | sockit |
version | 0.2.1 |
source | src |
created_at | 2023-03-20 15:15:15.941252 |
updated_at | 2023-03-30 01:48:28.00212 |
description | A UDP socket that can read and write serializable data |
homepage | |
repository | |
max_upload_size | |
id | 815358 |
size | 10,173 |
A high-level UDP Socket that allows for writing and reading (de)serializable values
Add this to your Cargo.toml:
[dependencies]
sockit = "0.2.1"
You can create a Socket by binding it to an address like so:
#[tokio::main]
async fn main() {
let socket = sockit::UdpSocket::bind("127.0.0.1:0").await?;
}
You can use the Socket to send and receive serializable objects:
use sockit::UdpSocket;
use serde::{Serialize, Deserialize};
/// A (de)serializable type shared between client and server
#[derive(Serialize, Deserialize)]
struct Message {
id: u32,
data: String,
}
/// Code running client side
async fn client_side(mut client_socket: UdpSocket) {
let message = Message {
id: 1,
data: "Hello, world!".to_string(),
};
client_socket.write::<Message>(&message).await.unwrap();
}
/// Code running server side
async fn server_side(mut server_socket: UdpSocket) {
let message: Message = server_socket.read::<Message>().await.unwrap().unwrap();
}