How to do worker-to-worker communication?

后端 未结 1 714
一生所求
一生所求 2020-12-30 08:37

I\'m experimenting with web workers, and was wondering how well they would deal with embarassingly parallell problems. I therefore implemented Connaway\'s Game of Life. (To

相关标签:
1条回答
  • 2020-12-30 09:27

    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.

    0 讨论(0)
提交回复
热议问题