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