From cf3c3958507e651b8fbb5687635aba2c66d208ec Mon Sep 17 00:00:00 2001 From: Yehowshua Immanuel Date: Tue, 7 Jan 2025 08:54:07 -0500 Subject: [PATCH] structural improvements --- README.md | 1 + backend/src/landing.rs | 23 ++++++++++++++++++----- backend/src/main.rs | 31 ++++++++++++++++--------------- 3 files changed, 35 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index 007244b..a0b949e 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,7 @@ Now open `http://127.0.0.1:8080` in your browser. - [ ] `EventHandlers.onMessage` should inject successfully decoded message into Msg type directly... - [ ] Run `uglify` twice as per [this link](https://github.com/rtfeldman/elm-spa-example/tree/master?tab=readme-ov-file#production-build) + - [ ] Frontend should initiate with TimeRequest - [ ] JSONify backend code for all send/receive - [ ] Backend should only communicate over websocket - [ ] Close websocket after 15s of no response(from frontend) to websocket pings diff --git a/backend/src/landing.rs b/backend/src/landing.rs index 163bbe2..273f3c1 100644 --- a/backend/src/landing.rs +++ b/backend/src/landing.rs @@ -1,14 +1,27 @@ +use actix_web_actors::ws::WebsocketContext; use serde::{Deserialize, Serialize}; - -#[derive(Serialize)] +#[derive(Serialize, Debug)] pub enum DownMsg { Greeting(String), TimeUpdate(String), } - -#[derive(Deserialize)] +#[derive(Deserialize, Debug)] pub enum UpMsg { - RequestGreet(String) + RequestGreet(String), +} + +pub fn msg_handler( + msg: UpMsg, + ws: &mut crate::MyWebSocket, + ctx: &mut WebsocketContext, +) { + match msg { + UpMsg::RequestGreet(name) => { + let greeting = format!("Hello, {}!", name); + let response = crate::DownMsg::Landing(DownMsg::Greeting(greeting)); + ws.send_down_msg(response, ctx); + } + } } diff --git a/backend/src/main.rs b/backend/src/main.rs index 4e1f604..e58723c 100644 --- a/backend/src/main.rs +++ b/backend/src/main.rs @@ -2,25 +2,24 @@ use actix::prelude::*; use actix_files::Files; use actix_web::{web, App, Error, HttpRequest, HttpResponse, HttpServer, Responder}; use actix_web_actors::ws; +use chrono::Local; use log::info; use serde::{Deserialize, Serialize}; use std::{fs, time::Duration}; -use chrono::Local; mod landing; -#[derive(Serialize)] -enum DownMsg { +#[derive(Serialize, Debug)] +pub enum DownMsg { Landing(landing::DownMsg), } -#[derive(Deserialize)] +#[derive(Deserialize, Debug)] enum UpMsg { Landing(landing::UpMsg), } -/// WebSocket actor -struct MyWebSocket; +pub struct MyWebSocket; impl Actor for MyWebSocket { type Context = ws::WebsocketContext; @@ -46,17 +45,10 @@ impl StreamHandler> for MyWebSocket { ) { match msg { Ok(ws::Message::Text(text)) => { - // Attempt to decode the incoming message if let Ok(up_msg) = serde_json::from_str::(&text) { match up_msg { - UpMsg::Landing(landing::UpMsg::RequestGreet(name)) => { - let greeting = format!("Hello, {}!", name); - let response = DownMsg::Landing(landing::DownMsg::Greeting(greeting)); - - if let Ok(json_response) = serde_json::to_string(&response) { - ctx.text(json_response); - } - } + UpMsg::Landing(msg) => + landing::msg_handler(msg, self, ctx), } } } @@ -68,6 +60,15 @@ impl StreamHandler> for MyWebSocket { } } +impl MyWebSocket { + fn send_down_msg(&self, down_msg: DownMsg, ctx: &mut ws::WebsocketContext) { + if let Ok(serialized_msg) = serde_json::to_string(&down_msg) { + ctx.text(serialized_msg); + } else { + log::error!("Failed to serialize DownMsg {:?}", down_msg); + } + } +} async fn websocket_handler(req: HttpRequest, stream: web::Payload) -> Result { ws::start(MyWebSocket {}, &req, stream)