use std::collections::HashMap;

use simple_websockets::{Event, Responder};

fn main() {
    let event_hub = simple_websockets::launch(8080).expect("failed to listen on port 8080");
    let mut clients: HashMap<u64, Responder> = HashMap::new();
    println!("Listening for websocket connection...");
    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);
            }
        }
    }
}
