use std::{ sync::Arc, io::Error as IoError, }; use tokio::{ net::{TcpListener}, }; use hexland_server::GlobalState; use hexland_server::message::MessageWebSocket; mod client; use client::Client; #[tokio::main] async fn main() -> Result<(), IoError> { let global_state = Arc::new(GlobalState::default()); // Bind socket let socket = TcpListener::bind("127.0.0.1:8080").await; let listener = socket.expect("Could not bind to localhost:8080"); println!("Server running"); // Accept all incoming connections while let Ok((stream, addr)) = listener.accept().await { let global_state = Arc::clone(&global_state); tokio::spawn(async move { // Upgrade to a WS connection let ws = MessageWebSocket(tokio_tungstenite::accept_async(stream) .await .expect("Could not establish connection")); println!("Connected to {}", addr); Client::new(ws, global_state).run().await; }); } Ok(()) }