webui-dev/webui

webui_wait() returns when device goes to sleep.

Closed this issue · 3 comments

Description

works as expected when running normally, but disconnects when device goes to sleep.

Expected behavior

webui should not disconnect when device goes to sleep.

Relevant Snippets

use std::{ffi::CStr, sync::Arc};
use webui_rs::webui;

#[derive(Clone)]
pub struct App {
    pub win: Arc<webui::Window>,
}
impl App {
    pub fn new() -> Self {
        Self {
            win: Arc::new(webui::Window::new()),
        }
    }

    pub async fn open_window(&self, url: String) -> anyhow::Result<()> {
        unsafe {
            let _ = webui::bindgen::webui_set_port(
                self.win.id,
                core::env!("WEBUI_PORT").parse().unwrap(),
            );
        }
        webui::set_timeout(3);
        self.win.bind("", |e| {
            dbg!(e.event_type as u32);
        });

        let s = self.clone();
        tokio::task::spawn_blocking(move || {
            s.win.show(url);
            webui::wait();
        })
        .await?;

        Ok(())
    }

    pub fn close(&self) {
        self.win.close();
    }
}

#[tokio::main]
async fn main() -> Result<()> {
    let port: u16 = core::env!("SERVER_PORT").parse().unwrap();
    let mut url = format!("http://localhost:{}/", port);

    let app = webui::App::new();

    tokio::select! {
        window = app.open_window(url) => {
            app.close();
            window?;
        }
    }

    Ok(())
}

Logs

[src/webui.rs:48:13] e.event_type as u32 = 2
[src/webui.rs:48:13] e.event_type as u32 = 2
[src/webui.rs:48:13] e.event_type as u32 = 0

Environment info

OS Version: Linux NixOS 24.05

Compiler Version: Rust 1.77.1

WebUI Version: 2.5.0-beta.1

Thank you for the report, I'm afraid that is issue is hard to fix due to OS low-power state settings, all WebSocket connections in all opened web sites are typically disrupted, perhaps we can add in the future a re-connection API in webui.js.

Thanks for responding :)

I don't know if it is possible, but i was thinking about checking if the web browser process is alive or not before returning from webui_wait().

websockets from webui.js reconnect just fine if the application (my rust app) is left alive. the only problem is that it's hard to figure out if we should end the app when webui_wait() returns or not.

Bridge webui.js updated (faa02ad). Now, it will try to re-connect.

I tried also to improve wait() to not return if OS is in sleep mode, but this will make WebUI depend on some extras system's lib. I left it to the developer to add his own version.

webui_show(win, "index.html");
while (inSleepMode()) {
    webui_wait();
}

Another solution, you can use WebView and set wait() to never return, and you should have an Exit button in the UI.

webui_set_timeout(0);
webui_show_browser(win, "index.html", AnyBrowser);
webui_wait();