Skip to content

Commit 3518092

Browse files
committed
feat: exit when ws connection dropped
When the websocket connection drops, previous behaviour was that the infinite loop tried to catch it and retry the connection. this was spotty (probably because of wifi) so to fix this change to so it quits and then let systemd restart it
1 parent 282eb0f commit 3518092

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

control/src/main.rs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,18 @@ fn main() -> Result<(), Box<dyn Error>> {
3333
let (frame_recv, mut frame_send) =
3434
single_value_channel::channel_starting_with(image::DynamicImage::new_rgb8(120, 80));
3535

36-
std::thread::spawn(move || sender_thread(frame_recv, scene));
37-
38-
loop {
39-
let r = inner(&opt, &mut frame_send);
40-
41-
println!("Inner loop failed: {:#?}", r);
42-
43-
std::thread::sleep(Duration::from_secs(5));
36+
let sender_handle = std::thread::spawn(move || sender_thread(frame_recv, scene));
37+
38+
// Single attempt to connect and process messages
39+
let result = inner(&opt, &mut frame_send);
40+
41+
// If the connection drops or fails, print the error and exit
42+
if let Err(e) = result {
43+
eprintln!("WebSocket connection failed: {:#?}", e);
4444
}
45+
46+
// Exit the program instead of retrying
47+
std::process::exit(1)
4548
}
4649

4750
fn sender_thread(
@@ -87,5 +90,6 @@ fn inner(
8790
frame_chan.update(frame)?;
8891
}
8992

90-
Ok(())
93+
// If we exit the loop (connection closed), return an error
94+
Err("WebSocket connection closed".into())
9195
}

0 commit comments

Comments
 (0)