basic listener implemented, addedd modules, working on handling connections
This commit is contained in:
@@ -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"
|
||||
|
||||
@@ -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) {}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user