hexland-server/src/main.rs

37 lines
1.1 KiB
Rust
Raw Normal View History

2022-10-12 12:48:23 -05:00
use std::{io::Error as IoError, sync::Arc};
2022-10-04 16:59:12 -05:00
2022-10-12 12:48:23 -05:00
use tokio::net::TcpListener;
2022-10-12 12:48:23 -05:00
use hexland_server::GlobalState;
2022-10-12 16:57:48 -05:00
use hexland_server::client::Client;
use hexland_server::message::MessageWebSocket;
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());
// 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");
// 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 12:48:23 -05:00
let ws = MessageWebSocket(
tokio_tungstenite::accept_async(stream)
.await
.expect("Could not establish connection"),
);
2022-10-04 16:59:12 -05:00
println!("Connected to {}", addr);
2022-10-12 10:57:57 -05:00
Client::new(ws, global_state).run().await;
2022-10-12 12:51:16 -05:00
unreachable!();
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(())
}