From f9224ff02e688dec819ab81893320a0611f2a198 Mon Sep 17 00:00:00 2001 From: FxQnLr Date: Tue, 24 Oct 2023 14:56:17 +0200 Subject: Seems to work --- src/services/ping.rs | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) (limited to 'src/services/ping.rs') diff --git a/src/services/ping.rs b/src/services/ping.rs index 6e710ec..ff328a5 100644 --- a/src/services/ping.rs +++ b/src/services/ping.rs @@ -1,8 +1,9 @@ +use std::borrow::Cow; use std::sync::Arc; use axum::{extract::{WebSocketUpgrade, ws::WebSocket, State}, response::Response}; -use tokio::sync::mpsc::Sender; -use tracing::{debug, error}; +use tokio::sync::broadcast::{Sender}; +use tracing::{debug, error, trace}; use crate::{error::WebolError, AppState}; @@ -12,7 +13,7 @@ pub async fn spawn(tx: Sender) -> Result<(), WebolError> { let mut cont = true; while cont { let ping = surge_ping::ping( - "192.168.178.28".parse().map_err(WebolError::IpParse)?, + "127.0.0.1".parse().map_err(WebolError::IpParse)?, &payload ).await; @@ -30,24 +31,29 @@ pub async fn spawn(tx: Sender) -> Result<(), WebolError> { debug!("Ping took {:?}", duration); cont = false; // FIXME: remove unwrap - tx.send("Got ping".to_string()).await.unwrap(); + tx.send("Got ping".to_string()).unwrap(); }; } Ok(()) } -pub async fn ws_ping(ws: WebSocketUpgrade, State(_state): State>) -> Response { - ws.on_upgrade(handle_socket) +// TODO: Status to routes, websocket here +pub async fn ws_ping(State(state): State>, ws: WebSocketUpgrade) -> Response { + ws.on_upgrade(move |socket| handle_socket(socket, state.ping_send.clone())) } // FIXME: Handle commands through enum -async fn handle_socket(mut socket: WebSocket) { +async fn handle_socket(mut socket: WebSocket, tx: Sender) { // TODO: Understand Cow - - // match socket.send(axum::extract::ws::Message::Close(Some(CloseFrame { code: 4000, reason: Cow::Owned("started".to_owned()) }))).await.map_err(WebolError::Axum) { - match socket.send(axum::extract::ws::Message::Text("started".to_string())).await.map_err(WebolError::Axum) { + while let message = tx.subscribe().recv().await.unwrap() { + trace!("GOT = {}", message); + if &message == "Got ping" { + break; + } + }; + match socket.send(axum::extract::ws::Message::Close(Some(axum::extract::ws::CloseFrame { code: 4000, reason: Cow::Owned("started".to_owned()) }))).await.map_err(WebolError::Axum) { Ok(..) => (), Err(err) => { error!("Server Error: {:?}", err) } }; -} +} \ No newline at end of file -- cgit v1.2.3