Skip to content

Instantly share code, notes, and snippets.

@fijiwebdesign
Last active February 12, 2017 09:58
Show Gist options
  • Save fijiwebdesign/48377e5e550f0f757361f53e9d150375 to your computer and use it in GitHub Desktop.
Save fijiwebdesign/48377e5e550f0f757361f53e9d150375 to your computer and use it in GitHub Desktop.
Shared WebWorker Example
<script>
var url = 'shared-webworker.js';
var worker = new SharedWorker(url);
console.log('Using shared worker at', url)
worker.port.addEventListener("message", function(e) {
alert(e.data);
}, false);
worker.port.start();
</script>
/**
* Shared Web Worker
* see: https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Using_web_workers
* https://www.sitepoint.com/javascript-shared-web-workers-html5/
*/
var connections = 0;
self.addEventListener("connect", function (e) {
var port = e.ports[0];
connections++;
port.postMessage("hello " + e.data + " (port #" + connections + ") " + JSON.stringify(port));
port.addEventListener("message", function (e) {
port.postMessage("Message " + e.data + " (port #" + connections + ")");
}, false);
port.start();
}, false);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment