I have a background worker thread which is communicating via XMLHttpRequest to our web server and this has been working well. That said I am having problems shutting down this worker cleanly on exit of the page. From the main page I post a stop message to the Worker and it does receive it. But when the worker attempts to perform one final XMLHttpRequest to "LogOut" from the server, this request is always failing.
Are worker threads prevented from processing network requests after the onbeforeunload event is triggered for the main page? Is there some other way to do this such that I can get a clean disconnect from the server?
I have been doing this for years from the main pages without a problem, but the Worker threads seem to have some nasty restrictions unless I am doing something wrong. The same code works just fine if I post the Stop message prior to the onbeforeunload of the user exiting the page.
Main Page:
$(window).on("beforeunload", function () {
worker.postMessage({ Action: "Stop" });
});
Worker Script:
self.addEventListener("message", function (d) {
switch(d.data.Action) {
case "Stop":
// Simple REST GET code for logout which always fails
break;
}
});