diff --git a/src/websocket.rs b/src/websocket.rs index e39694c..c5bf991 100644 --- a/src/websocket.rs +++ b/src/websocket.rs @@ -1,6 +1,6 @@ use crate::config::{Context, WebSocketCfg}; use crate::ip::IpEvent; -use crate::utils::gethostname; +use crate::utils::{gethostname, sleep_s}; use serde_json::json; use std::io::{self, Write}; @@ -44,7 +44,12 @@ pub async fn websocketpubsub( let mut ws = websocket.write().await; match ws.read_message() { Ok(msg) => { - let tosend: IpEvent = serde_json::from_str(msg.to_string().as_str()).unwrap(); + let tosend: IpEvent = match serde_json::from_str(msg.to_string().as_str()) { + Ok(o) => o, + Err(_e) => { + continue; + } + }; if tosend.ipdata.hostname != gethostname(true) || tosend.msgtype == "init".to_string() { @@ -71,7 +76,18 @@ pub async fn websocketconnect<'a>( ) -> Result>, Error> { print!("connecting to {} ...", &wscfg.endpoint); io::stdout().flush().unwrap(); - let (mut socket, _response) = connect(&wscfg.endpoint).expect("Can't connect"); + let mut socket; + loop { + (socket, _) = match connect(&wscfg.endpoint) { + Ok((o, e)) => (o, e), + _ => { + println!("error connecting, retrying"); + sleep_s(1).await; + continue; + } + }; + break; + } print!(" connected!\n"); let msg = json!({ "hostname": hostname }); socket