Note that there are some explanatory texts on larger screens.

plurals
  1. POMessage Channel in Shared Workers
    primarykey
    data
    text
    <p>Does Message Channel work in Shared Workers? I want to create some kind of communication between several Shared Workers, so I took example from <a href="http://www.antkorp.in/workersComm/" rel="nofollow">here</a> and change it into this:</p> <pre><code>$(function() { var worker1 = new SharedWorker("worker1.js"); var worker2 = new SharedWorker("worker2.js"); var channel=new MessageChannel(); worker1.port.start(); worker2.port.start(); worker1.port.onmessage = function(e) { $("#log").append("&lt;br&gt;" + e.data); console.log(e.data); } worker1.port.postMessage({ code : "start", msg : "ping" },[channel.port1]); worker2.port.onmessage = function(e) { $("#log").append("&lt;br&gt;" + e.data); console.log(e.data); } worker2.port.postMessage({ code : "start", msg : "ping" },[channel.port2]); /*channel.port1.postMessage("channel port1 msg"); channel.port1.onmessage=function(e){ $("#log").append("&lt;br&gt; port1 recieved msg : " + e.data); console.log(e.data); }*/ $("#send1").click(function() { var msg = $("#msg").val(); if (msg &amp;&amp; msg != "start") worker1.port.postMessage({ code : "msgw", msg : "ping2" }); $("#msg").val(""); }) $("#send2").click(function() { var msg = $("#msg").val(); if (msg &amp;&amp; msg != "start") worker2.port.postMessage({ code : "msgw", msg : "ping3" }); $("#msg").val(""); }) $("#send3").click(function() { var msg = $("#msg").val(); if (msg &amp;&amp; msg != "start") worker1.port.postMessage({ code : "msgch", msg : "ping3" }); $("#msg").val(""); }); $("#send4").click(function() { var msg = $("#msg").val(); if (msg &amp;&amp; msg != "start") worker2.port.postMessage({ code : "msgch", msg : "ping3" }); $("#msg").val(""); }) }); </code></pre> <p>worker1.js and worker2.js:</p> <pre><code>var channelPort; function getChannelMessage(e){ postMessage(e.data+" &gt;&gt; channel recieved msg in worker1 "); } self.onconnect = function(e){ var port = e.ports[0]; port.onmessage = function(e) { if (e.data.code == "start") { channelPort = e.ports[0]; channelPort.start(); channelPort.postMessage(e.data.msg+" &gt;&gt; worker1 channel post"); channelPort.onmessage = getChannelMessage; } else if(e.data.code=="msgw") { port.postMessage(e.data.msg+" &gt;&gt; worker1 got msg"); }else if(e.data.code=="msgch") { channelPort.postMessage(e.data.msg+" &gt;&gt; worker1 got msg"); } }; port.start(); } </code></pre> <p>But when I send message from worker1 I don't receive it in worker2. Am I doing something wrong or is it impossible to do? Is there any other way of communicating two Shared Workers without posting message to browser tab?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload