37 lines
1.1 KiB
Rust
37 lines
1.1 KiB
Rust
use std::{io::Error as IoError, sync::Arc};
|
|
|
|
use tokio::net::TcpListener;
|
|
|
|
use hexland_server::GlobalState;
|
|
use hexland_server::client::Client;
|
|
use hexland_server::message::MessageWebSocket;
|
|
|
|
|
|
#[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;
|
|
|
|
unreachable!();
|
|
});
|
|
}
|
|
Ok(())
|
|
}
|