Skip to content

Instantly share code, notes, and snippets.

@leon0707
Last active February 4, 2020 14:52
Show Gist options
  • Save leon0707/13001e2c9923fad7b5e8c948579970d8 to your computer and use it in GitHub Desktop.
Save leon0707/13001e2c9923fad7b5e8c948579970d8 to your computer and use it in GitHub Desktop.

Revisions

  1. leon0707 revised this gist Feb 4, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion shared-worker.js
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    // shared-worker.js
    onconnect = function(e) {
    var port = e.ports[0];
    port.onmessage = function(e) {
    port.onmessage = function(e) {
    port.postMessage('send msg back to a script');
    }
    }
  2. leon0707 created this gist Feb 4, 2020.
    15 changes: 15 additions & 0 deletions shared-worker.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,15 @@
    // shared-worker.js
    onconnect = function(e) {
    var port = e.ports[0];
    port.onmessage = function(e) {
    port.postMessage('send msg back to a script');
    }
    }
    // client
    if (!!window.SharedWorker) {
    const myWorker = new SharedWorker("shared-worker.js");
    myWorker.port.postMessage('msg to a shared worker');
    myWorker.port.onmessage = function(e) {
    console.log(e.lastEventId);
    }
    }