Add initial server implementation
This adventure is going to be epic, be ready.
This commit is contained in:
@@ -2,4 +2,6 @@
|
||||
members = [
|
||||
"board-shared",
|
||||
"board-frontend",
|
||||
"board-server",
|
||||
"board-network",
|
||||
]
|
||||
|
10
board-network/Cargo.toml
Normal file
10
board-network/Cargo.toml
Normal file
@@ -0,0 +1,10 @@
|
||||
[package]
|
||||
name = "board-network"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
serde = { version = "1.0.152", features = ["derive"] }
|
||||
board-shared = { path = "../board-shared" }
|
1
board-network/src/lib.rs
Normal file
1
board-network/src/lib.rs
Normal file
@@ -0,0 +1 @@
|
||||
pub mod protocol;
|
15
board-network/src/protocol.rs
Normal file
15
board-network/src/protocol.rs
Normal file
@@ -0,0 +1,15 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Debug, Deserialize, Eq, PartialEq, Serialize)]
|
||||
pub enum ClientMessage {
|
||||
CreateRoom(String),
|
||||
JoinRoom(String, String),
|
||||
Disconnected,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
|
||||
pub enum ServerMessage {
|
||||
JoinedRoom {
|
||||
room_name: String,
|
||||
},
|
||||
}
|
15
board-server/Cargo.toml
Normal file
15
board-server/Cargo.toml
Normal file
@@ -0,0 +1,15 @@
|
||||
[package]
|
||||
name = "board-server"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
futures = { version = "0.3" }
|
||||
board-shared = { path = "../board-shared" }
|
||||
board-network = { path = "../board-network" }
|
||||
smol = "1.3.0"
|
||||
async-tungstenite = "0.20.0"
|
||||
tungstenite = "0.18.0"
|
||||
anyhow = "1.0.69"
|
42
board-server/src/main.rs
Normal file
42
board-server/src/main.rs
Normal file
@@ -0,0 +1,42 @@
|
||||
use anyhow::Result;
|
||||
use futures::StreamExt;
|
||||
use smol::Async;
|
||||
use std::net::{SocketAddr, TcpListener, TcpStream};
|
||||
use std::{env, io};
|
||||
use tungstenite::Message as WebsocketMessage;
|
||||
|
||||
async fn handle_connection(raw_stream: Async<TcpStream>, addr: SocketAddr) -> Result<()> {
|
||||
println!("[{addr}] Incoming TCP connection");
|
||||
let mut ws_stream = async_tungstenite::accept_async(raw_stream).await?;
|
||||
println!("[{addr}] WebSocket connection established");
|
||||
|
||||
while let Some(Ok(WebsocketMessage::Text(text))) = ws_stream.next().await {
|
||||
println!("[{addr}] Received message: {text}");
|
||||
}
|
||||
|
||||
println!("[{addr}] Dropping connection");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn main() -> Result<(), io::Error> {
|
||||
let addr = env::args()
|
||||
.nth(1)
|
||||
.unwrap_or_else(|| "0.0.0.0:8080".to_string())
|
||||
.parse::<SocketAddr>()
|
||||
.expect("Invalid address");
|
||||
|
||||
smol::block_on(async {
|
||||
println!("Listening on: {addr}");
|
||||
let listener = Async::<TcpListener>::bind(addr).expect("Could not create listener");
|
||||
|
||||
while let Ok((stream, addr)) = listener.accept().await {
|
||||
smol::spawn(async move {
|
||||
if let Err(e) = handle_connection(stream, addr).await {
|
||||
eprintln!("Failed to handle connection from {addr}: {e}");
|
||||
}
|
||||
})
|
||||
.detach();
|
||||
}
|
||||
});
|
||||
Ok(())
|
||||
}
|
Reference in New Issue
Block a user