lasselukkari / aWOT

Arduino web server library.
MIT License
283 stars 41 forks source link

Question: how to close the response before rebooting the device #148

Closed raffaeler closed 5 months ago

raffaeler commented 5 months ago

Hi, I am trying to send a complete response and close it before a reboot. Unfortunately, the client never see the response coming. The ESP32 close the connection before flushing the full response out.

What I tried:

// res is of type aWot::Response
res.set("Connection", "close");
res.flush();
res.status(200);
res.end();
serveClient(...); // serving the client as for the examples
server.flush();
reboot();

In a similar code using the WebServer class, I never had this kind of issues.

What am I missing? TIA

lasselukkari commented 5 months ago

Add bool shouldReboot = false; to global scope and then in the handler set it to true instead of directly calling the reboot();. Then in your main loop check if shouldReboot is true and call the reboot(); there. Depending on your platform you may need to add some delay before running the reboot to make sure the response has actually been sent to the client.

raffaeler commented 5 months ago

This is exactly the current workaround :-) I was wondering what background tasks need to be executed and if there is any way to trigger them instead (aesthetic reason for my code).

Thanks!

lasselukkari commented 5 months ago

If you are closing the connection already just adding the delay may do the trick. For example the ESP platforms are multithreaded and the actual data sending happens async. if you reboot immediately the response may not have been actually sent.

raffaeler commented 5 months ago

I suspected and the last call to rebooting has a delay inside, but I believe that threads get stopped and thus also the web activity.

lasselukkari commented 5 months ago

With most platforms the delay should call yield internally and let all the os run whatever is needed in the meanwhile. You may try to flush the response before calling reboot. But all this depends how the tcp client is implemented in the used platform. Doing it in the main loop is the most safe way to do this.

raffaeler commented 5 months ago

Got it, thank you!