How to do worker-to-worker communication?

半腔热情 提交于 2019-11-30 07:08:52

You should be able to use channel messaging:

var channel = new MessageChannel();
worker1.postMessage({code:"port"}, [channel.port1]);
worker2.postMessage({code:"port"}, [channel.port2]);

Then in your worker threads:

var xWorkerPort;
onmessage = function(event) {
    if (event.data.code == "port") {
        xWorkerPort = event.ports[0];
        xWorkerPort.onmessage = function(event) { /* do stuff */ };
    }
}

There's not much documentation around, but you could try this MS summary to get started.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!