updated error handling
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Paul 2023-04-13 23:07:42 +02:00
parent f557e36941
commit b1b5f4ef7d

View File

@ -1,6 +1,6 @@
use crate::config::{Context, WebSocketCfg}; use crate::config::{Context, WebSocketCfg};
use crate::ip::IpEvent; use crate::ip::IpEvent;
use crate::utils::gethostname; use crate::utils::{gethostname, sleep_s};
use serde_json::json; use serde_json::json;
use std::io::{self, Write}; use std::io::{self, Write};
@ -44,7 +44,12 @@ pub async fn websocketpubsub(
let mut ws = websocket.write().await; let mut ws = websocket.write().await;
match ws.read_message() { match ws.read_message() {
Ok(msg) => { 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) if tosend.ipdata.hostname != gethostname(true)
|| tosend.msgtype == "init".to_string() || tosend.msgtype == "init".to_string()
{ {
@ -71,7 +76,18 @@ pub async fn websocketconnect<'a>(
) -> Result<WebSocket<MaybeTlsStream<TcpStream>>, Error> { ) -> Result<WebSocket<MaybeTlsStream<TcpStream>>, Error> {
print!("connecting to {} ...", &wscfg.endpoint); print!("connecting to {} ...", &wscfg.endpoint);
io::stdout().flush().unwrap(); 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"); print!(" connected!\n");
let msg = json!({ "hostname": hostname }); let msg = json!({ "hostname": hostname });
socket socket