Nano_get handle error

How you do it?

What are you confused about? It would look something like this:

fn every_60() {
    loop {
        check_server();
        std::thread::sleep(Duration::from_secs(60));
    }
}

fn check_server() {
    let request = nano_get::Request::default_get_request("https:/test.com").expect("Failed to open request");
    let response: Response = match request.execute() {
        Ok(x) => x,
        Err(err) => {
            println!("oops: {}", err);
            return;
        }
    };
    
    // look inside the response now
}

Thanks Alice

I'm sorry but I have an error in this function:

fn check_server() {
            let request = nano_get::Request::default_get_request("https://sharelogic.sharelock.it/SharelockWebApiRaspberry/Ping").expect("Failed to open request");
            let response: Response = match request.execute() {
                Ok(x) => x,
                Err(err) => {
                    println!("Network error: {}", err);
                    return;
                }
            };

            // look inside the response now
            if response.body.contains("PING OK") {
                let text = String::from("PING OK");
                let _ = sender.send(Message::UpdateLabel(String::from(text)));
                }
            }

can't capture dynamic environment in a fn item in sender

How can I use "sender" variable into this function?

Well why can't you use it?

Because can't capture dynamic environment in a fn item

I don't see any closures in the snippet you posted. Please post more context, and I would appreciate it if you posted it with correct indentation.

    enum Message {
        UpdateLabel(String),
    }
    // Create a new sender/receiver pair with default priority
    let (sender, receiver) = glib::MainContext::channel(glib::PRIORITY_DEFAULT);
    
    // Spawn the thread and move the sender in there
    thread::spawn(move || {
        fn every_60() {
            loop {
                check_server();
                let ping_polling: u64 = (select_ping_polling().expect("ping error")).parse::<u64>().unwrap();
                println!("{}", ping_polling);
                thread::sleep(time::Duration::from_secs(ping_polling));
            }
        }
        fn check_server() {
            let request = nano_get::Request::default_get_request("https://test.com").expect("Failed to open request");
            let response: Response = match request.execute() {
                Ok(x) => x,
                Err(err) => {
                    println!("Network error: {}", err);
                    return;
                }
            };
            
            // look inside the response now
            if response.body.contains("PING OK") {
                let text = String::from("PING OK");
                let _  = move || {sender.send(Message::UpdateLabel(String::from(text)));};
            }
        }
    });
    
    // Attach the receiver to the default main context (None)
    // and on every message update the label accordingly.
    let label_clone = label.clone();
    receiver.attach(None, move |msg| {
        match msg {
            Message::UpdateLabel(text) => label_clone.set_text(text.as_str()),
        }
        // Returning false here would close the receiver
        // and have senders fail
        glib::Continue(true)
    });

Did you .. put the functions inside the thread::spawn call?

yes.

Normally functions are defined outside other functions, and just called where you need them.

So you suggest to put check server in the main function?

The function definition, i.e. the fn check_server() part shouldn't be inside any other function at all. You should just call it by putting a check_server(); call whereever you want the code to jump to that function.

ok but if I change how you suggest, the line thread::sleep(time::Duration::from_secs(60)); sleep all my program?

Not if you call it from inside the new thread:

// outside any other function
fn every_60() {
    loop {
        check_server();
        let ping_polling: u64 = (select_ping_polling().expect("ping error")).parse::<u64>().unwrap();
        println!("{}", ping_polling);
        thread::sleep(time::Duration::from_secs(ping_polling));
    }
}
fn check_server() {
    let request = nano_get::Request::default_get_request("https://test.com").expect("Failed to open request");
    let response: Response = match request.execute() {
        Ok(x) => x,
        Err(err) => {
            println!("Network error: {}", err);
            return;
        }
    };
}

// later, somewhere else
thread::spawn(move || {
    every_60();
});

ok I try. thanks

Another problem :cry: variable "response" in "check_server" fn should change a glib label.
Do I have to call the function by passing the label as a parameter?

You can't use glib or gtk from multiple threads, so you probably wont be able to give the label as a parameter at all. You will probably have to use a channel like we discussed over here, and then pass the sender as an argument to my functions.

so I have to pass sender to function every_60? How do you pass?