#websocket #web #async #server #network

simple-websockets

Fast and efficient WebSocket server with an easy API

5 releases

Uses new Rust 2021

0.1.4 Nov 9, 2021
0.1.3 Oct 26, 2021
0.1.2 Oct 21, 2021
0.1.1 Mar 11, 2021
0.1.0 Mar 11, 2021

#31 in WebSocket

Download history 19/week @ 2022-03-13 20/week @ 2022-03-20 2/week @ 2022-03-27 9/week @ 2022-04-03 4/week @ 2022-04-10 7/week @ 2022-04-17 20/week @ 2022-04-24 31/week @ 2022-05-01 32/week @ 2022-05-08 90/week @ 2022-05-15 29/week @ 2022-05-22 42/week @ 2022-05-29 27/week @ 2022-06-05 3/week @ 2022-06-12 28/week @ 2022-06-19 18/week @ 2022-06-26

81 downloads per month

MIT license

13KB
170 lines

simple-websockets

An easy-to-use WebSocket server.

MIT licensed Docs Crates.io

Example

Echo server:

use simple_websockets::{Event, Responder};
use std::collections::HashMap;

fn main() {
    // listen for WebSockets on port 8080:
    let event_hub = simple_websockets::launch(8080)
        .expect("failed to listen on port 8080");
    // map between client ids and the client's `Responder`:
    let mut clients: HashMap<u64, Responder> = HashMap::new();

    loop {
        match event_hub.poll_event() {
            Event::Connect(client_id, responder) => {
                println!("A client connected with id #{}", client_id);
                // add their Responder to our `clients` map:
                clients.insert(client_id, responder);
            },
            Event::Disconnect(client_id) => {
                println!("Client #{} disconnected.", client_id);
                // remove the disconnected client from the clients map:
                clients.remove(&client_id);
            },
            Event::Message(client_id, message) => {
                println!("Received a message from client #{}: {:?}", client_id, message);
                // retrieve this client's `Responder`:
                let responder = clients.get(&client_id).unwrap();
                // echo the message back:
                responder.send(message);
            },
        }
    }
}

Dependencies

~6–13MB
~249K SLoC