sockit

Crates.iosockit
lib.rssockit
version0.2.1
sourcesrc
created_at2023-03-20 15:15:15.941252
updated_at2023-03-30 01:48:28.00212
descriptionA UDP socket that can read and write serializable data
homepage
repository
max_upload_size
id815358
size10,173
Will Cygan (wcygan)

documentation

README

Sockit 🧦

github crates.io docs.rs build status codecov

A high-level UDP Socket that allows for writing and reading (de)serializable values

Usage

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();
}
Commit count: 0

cargo fmt