#Detect if offline from Rust backend.

3 messages · Page 1 of 1 (latest)

fathom night
#

Is there a way to detect the network connection/network errors of a Webview from the backend (Rust)?

Context:

My Webview is hosting a remote URL, but I'd like to avoid showing the default ERR_INTERNET_DISCONNECTED screen when the user is offline.

If the user opens the application while offline, it will never connect to the remote host to load any JS. Therefore to cover all my bases, I'd need the App itself be able to tell when there are network issues. My thought would then be to follow a similar pattern to showing a "splashscreen", and closing it when the network connection of the main window is restored.

I'm fairly new to using Tauri (and Rust in general), so any help/suggestions appreciated.

void mantle
#

write a function that tries to fetch your website and if it fails then assume you're offline

#
use reqwest::blocking::get;
fn check_internet_connection() -> bool {
    let url = "https://www.google.com";
    match get(url) {
        Ok(_) => true,  // Successfully connected
        Err(_) => false, // Failed to connect
    }
}