diff --git a/client/main.js b/client/main.js index db8d6be..546884b 100644 --- a/client/main.js +++ b/client/main.js @@ -30,5 +30,10 @@ window.onload = function() { document.getElementById('content').src = presentURL; } }; -// connectWebSocket(); +// Add an event listener for the 'beforeunload' event, which is triggered when the page is refreshed or closed +window.addEventListener('beforeunload', () => { + // Send a termination signal to the worker before the page is unloaded + streamWorker.postMessage({ type: 'terminate' }); + eventWorker.postMessage({ type: 'terminate' }); +}); diff --git a/client/worker_event_processing.js b/client/worker_event_processing.js index 72eca3e..78f811d 100644 --- a/client/worker_event_processing.js +++ b/client/worker_event_processing.js @@ -24,7 +24,10 @@ onmessage = (event) => { rotate = event.data.rotate; // Handle the error, maybe show a user-friendly message or take some corrective action break; - + case 'terminate': + console.log("terminating worker"); + close(); + break; } }; diff --git a/client/worker_stream_processing.js b/client/worker_stream_processing.js index 64ffaea..031c64a 100644 --- a/client/worker_stream_processing.js +++ b/client/worker_stream_processing.js @@ -15,6 +15,11 @@ onmessage = (event) => { withColor = event.data.withColor; // Handle the error, maybe show a user-friendly message or take some corrective action break; + case 'terminate': + console.log("terminating worker"); + close(); + break; + } };