convert_to_actix #2

Merged
Yehowshua merged 20 commits from convert_to_actix into main 2025-01-06 06:25:19 +00:00
2 changed files with 26 additions and 21 deletions
Showing only changes of commit a6fd15c1b1 - Show all commits

View file

@ -5,8 +5,8 @@ use actix_web_actors::ws;
use log::info; use log::info;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::{fs, time::Duration}; use std::{fs, time::Duration};
use chrono::Local;
mod landing;
/// Greeting API structures /// Greeting API structures
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize)]
@ -19,9 +19,14 @@ struct GreetingResponse {
message: String, message: String,
} }
#[derive(Serialize)]
pub struct Landing {
pub time : String
}
#[derive(Serialize)] #[derive(Serialize)]
enum ToFrontend { enum ToFrontend {
Landing(landing::Landing) Landing(Landing)
} }
async fn greet(req: web::Json<GreetingRequest>) -> impl Responder { async fn greet(req: web::Json<GreetingRequest>) -> impl Responder {
@ -40,8 +45,16 @@ impl Actor for MyWebSocket {
info!("WebSocket actor started"); info!("WebSocket actor started");
// Send messages every second // Send messages every second
ctx.run_interval(Duration::from_secs(1), |_, ctx| { ctx.run_interval(Duration::from_secs(1), |_, ctx| {
let message = format!("{{\"time\" : \"{:?}\" }}", chrono::Local::now()); // Format the current time as HH:mm:ss
ctx.text(message); let current_time = Local::now().format("%H:%M:%S").to_string();
let message = ToFrontend::Landing(Landing { time: current_time });
// Serialize the message to JSON
if let Ok(json_message) = serde_json::to_string(&message) {
ctx.text(json_message);
} else {
info!("Failed to serialize the message");
}
}); });
info!("Leaving started"); info!("Leaving started");
} }

View file

@ -19,7 +19,7 @@ type alias Landing = {
} }
type Msg type Msg
= ToFrontend Landing = ToFrontend Landing
| Time String | DecodeError Decode.Error
| NoOp | NoOp
init : () -> Model init : () -> Model
@ -27,23 +27,15 @@ init flags = {
time = "time not yet set" time = "time not yet set"
} }
decodeMessage : String -> String decodeToFrontend message =
decodeMessage message =
let let
decodedMessage = Decode.decodeString (Decode.field "time" Decode.string) message decodeTime = Decode.field "time" Decode.string |> Decode.map Landing
decodeLanding = Decode.field "Landing" decodeTime |> Decode.map ToFrontend
decodedMessage = Decode.decodeString decodeLanding message
in in
case decodedMessage of case decodedMessage of
Ok decoded -> decoded Ok decoded -> decoded
Err err -> "failed to decode" ++ message Err err -> DecodeError err
decodeMessageOld : String -> String
decodeMessageOld message =
let
decodedMessage = Decode.decodeString (Decode.field "time" Decode.string) message
in
case decodedMessage of
Ok decoded -> decoded
Err err -> "failed to decode" ++ message
subscriptions : Model -> Sub Msg subscriptions : Model -> Sub Msg
subscriptions model = subscriptions model =
@ -52,19 +44,19 @@ subscriptions model =
(\_ -> NoOp) (\_ -> NoOp)
(\_ -> NoOp) (\_ -> NoOp)
(\_ -> NoOp) (\_ -> NoOp)
(\message -> (message.data |> decodeMessageOld |> Time)) (\message -> (message.data |> decodeToFrontend))
(\_ -> NoOp) (\_ -> NoOp)
) )
update : Msg -> Model -> (Model, Cmd Msg) update : Msg -> Model -> (Model, Cmd Msg)
update msg model = update msg model =
case msg of case msg of
Time time -> ( {model | time = time}, Cmd.none ) ToFrontend landing -> ( {model | time = landing.time}, Cmd.none )
_ -> (model, Cmd.none) _ -> (model, Cmd.none)
view : Model -> Element Msg view : Model -> Element Msg
view model = view model =
Element.column [] Element.column []
[ Element.text "Landing", [ Element.text "Landing",
Element.text <| "Time is : " ++ model.time Element.text <| "Current time is : " ++ model.time
] ]