basic listener implemented, addedd modules, working on handling connections

This commit is contained in:
2025-11-11 13:35:34 +01:00
parent 8f9a48fa96
commit 8ffbfdc63f
2 changed files with 39 additions and 2 deletions

View File

@@ -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"

View File

@@ -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::<Message>(txt) {}
}
}
});
}
}