diff --git a/.gitignore b/.gitignore old mode 100644 new mode 100755 index 4470988..ea8c4bf --- a/.gitignore +++ b/.gitignore @@ -1,2 +1 @@ -target/ -Cargo.lock \ No newline at end of file +/target diff --git a/Cargo.lock b/Cargo.lock new file mode 100755 index 0000000..12e8bb9 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "jade" +version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml old mode 100644 new mode 100755 index 97b7c61..30c7d10 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,8 +6,3 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -tokio = { version= "1", features = ["full"] } -tokio-stream = "0.1.6" -warp = "0.3" -futures = { version = "0.3", default-features=false} -uuid = { version = "0.4", features = ["serde", "v4"] } \ No newline at end of file diff --git a/src/main.rs b/src/main.rs old mode 100644 new mode 100755 index 2c36f44..e7a11a9 --- a/src/main.rs +++ b/src/main.rs @@ -1,31 +1,3 @@ -mod mods; -use std::{collections::HashMap, convert::Infallible, sync::Arc, env}; -use tokio::sync::{mpsc, Mutex}; -use warp::{ws::Message, Filter, Rejection}; - -#[derive(Debug, Clone)] -pub struct Client { - pub client_id: String, - pub sender: Option>>, -} - -type Clients = Arc>>; -type Result = std::result::Result; - -#[tokio::main] -async fn main() { - let clients: Clients = Arc::new(Mutex::new(HashMap::new())); - println!("Configuring websocket route"); - let ws_route = warp::path("ws") - .and(warp::ws()) - .and(with_clients(clients.clone())) - .and_then(mods::handlers::ws_handler); - - let routes = ws_route.with(warp::cors().allow_any_origin()); - println!("Starting server"); - warp::serve(routes).run(([127, 0, 0, 1], 8080)).await; -} - -fn with_clients(clients: Clients) -> impl Filter + Clone { - warp::any().map(move || clients.clone()) +fn main() { + println!("Hello, world!"); } diff --git a/src/mods.rs b/src/mods.rs deleted file mode 100644 index 3a3bd76..0000000 --- a/src/mods.rs +++ /dev/null @@ -1,2 +0,0 @@ -pub mod handlers; -pub mod ws; \ No newline at end of file diff --git a/src/mods/handlers.rs b/src/mods/handlers.rs deleted file mode 100644 index e5dc190..0000000 --- a/src/mods/handlers.rs +++ /dev/null @@ -1,8 +0,0 @@ -use crate::{mods::ws, Clients, Result}; -use warp::Reply; - -pub async fn ws_handler(ws: warp::ws::Ws, clients: Clients) -> Result { - println!("ws_handler"); - - Ok(ws.on_upgrade(move |socket| ws::client_connection(socket, clients))) -} \ No newline at end of file diff --git a/src/mods/ws.rs b/src/mods/ws.rs deleted file mode 100644 index d44d19a..0000000 --- a/src/mods/ws.rs +++ /dev/null @@ -1,63 +0,0 @@ -use crate::{Client, Clients}; -use futures::{FutureExt, StreamExt}; -use tokio::sync::mpsc; -use tokio_stream::wrappers::UnboundedReceiverStream; -use uuid::Uuid; -use warp::ws::{Message, WebSocket}; - - -pub async fn client_connection(ws: WebSocket, clients: Clients) { - println!("establishing client connection... {:?}", ws); - - let (client_ws_sender, mut client_ws_rcv) = ws.split(); - let (client_sender, client_rcv) = mpsc::unbounded_channel(); - - let client_rcv = UnboundedReceiverStream::new(client_rcv); - - tokio::task::spawn(client_rcv.forward(client_ws_sender).map(|result| { - if let Err(e) = result { - println!("error sending websocket msg: {}", e); - } - })); - - let uuid = Uuid::new_v4().simple().to_string(); - - let new_client = Client { - client_id: uuid.clone(), - sender: Some(client_sender), - }; - - clients.lock().await.insert(uuid.clone(), new_client); - while let Some(result) = client_ws_rcv.next().await { - let msg = match result { - Ok(msg) => msg, - Err(e) => { - println!("error receiving message for id {}): {}", uuid.clone(), e); - break; - } - }; - client_msg(&uuid, msg, &clients).await; - } - clients.lock().await.remove(&uuid); - println!("{} disconnected", uuid); -} - -async fn client_msg(client_id: &str, msg: Message, clients: &Clients) { - println!("received message from {}: {:?}", client_id, msg); - - let message = match msg.to_str() { - Ok(v) => v, - Err(_) => return, - }; - let locked = clients.lock().await; - match locked.get(client_id) { - Some(v) => { - if let Some(sender) = &v.sender { - println!("sending pong"); - let _ = sender.send(Ok(Message::text(message))); - } - } - None => return, - } - return; -} \ No newline at end of file