From 8ffbfdc63f49ed5f285520499fe8782ba1cdc5e9 Mon Sep 17 00:00:00 2001 From: htom Date: Tue, 11 Nov 2025 13:35:34 +0100 Subject: [PATCH] basic listener implemented, addedd modules, working on handling connections --- server/Cargo.toml | 7 +++++++ server/src/main.rs | 34 ++++++++++++++++++++++++++++++++-- 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/server/Cargo.toml b/server/Cargo.toml index ff06670..d37bf87 100644 --- a/server/Cargo.toml +++ b/server/Cargo.toml @@ -4,3 +4,10 @@ version = "0.1.0" edition = "2024" [dependencies] +tokio = { version = "1", features = ["full"] } +tokio-tungstenite = "0.21" +tungstenite = "0.21" +serde = { version = "1", features = ["derive"] } +serde_json = "1" +futures-util = "0.3.31" +url = "2.5.7" diff --git a/server/src/main.rs b/server/src/main.rs index e7a11a9..cca71d3 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -1,3 +1,33 @@ -fn main() { - println!("Hello, world!"); +use futures_util::{SinkExt, StreamExt}; +use serde::{Deserialize, Serialize}; +use tokio::net::TcpListener; +use tokio_tungstenite::accept_async; + +#[derive(Serialize, Deserialize, Debug)] +struct Message { + username: String, + text: String, +} + +#[tokio::main] +async fn main() { + let address = "0.0.0.0:9001"; //accept connection from anywhere + let listener = TcpListener::bind(address).await.unwrap(); + println!("Server running on ws://{}", address); + + while let Ok((stream, _)) = listener.accept().await { + println!("New connection!"); + + tokio::spawn(async move { + let ws_stream = accept_async(stream).await.unwrap(); + let (mut write, mut read) = ws_stream.split(); //seperate the read and write channel + + while let Some(Ok(msg)) = read.next().await { + if msg.is_text() { + let txt = msg.to_text().unwrap(); + if let Ok(json) = serde_json::from_str::(txt) {} + } + } + }); + } }