nautilus-sockets

Crates.ionautilus-sockets
lib.rsnautilus-sockets
version0.1.1
sourcesrc
created_at2024-11-14 22:51:36.951462
updated_at2024-11-21 16:28:59.847289
descriptionA UDP Socket that listens for events
homepage
repositoryhttps://github.com/philip727/nautilus-sockets
max_upload_size
id1448410
size58,803
philip (philip727)

documentation

README

Nautilus Sockets

A simple event based UDP socket listener

What is a nautilus socket (event based socket listener)?

The socket waits on specific events to be recieved and will emit an event to call all callbacks registered to that event. For example, if we were to recieve a "send_position" event, everything callback registered to that event would be called.

Examples

// Creates a socket using the NautServer implementation
let mut socket = NautSocket::<NautServer>::new("127.0.0.1:8080", ServerConfig::default())?;
// A hashmap containing all the positions
let positions: Arc<Mutex<HashMap<ConnectionId, Vector3>>> = Arc::new(Mutex::new(HashMap::new()));

let position_clone = Arc::clone(&positions);
// Everytime we recieve a packet with this event, it will perform this callback
socket.on("recieve_position", move |server, (addr, packet)| {
    // Gets a lockguard for the positions
    let Ok(mut positions) = position_clone.lock() else {
        return;
    };

    // Gets the client id based on the address that sent the packet
    let client = server.get_client_id(&addr);
    /// Assuming we have some form of deserialization method
    let vec3 = Vector3::from_bytes(&packet);
    positions.insert(client, vec3);
});

Commit count: 50

cargo fmt