javascript - How to call shared worker from the web worker? -
is possible call shared worker web worker?
could please give me example.
in case have few web workers , need share singleton service between them.
the sharedworker
constructor not available within workerglobalscope
, not able construct instance in iframe or window.
what can is, create messagechannel each of workers, , use communicate between worker , sharedworker. though doing negate need actual sharedworker
, since use single worker
instead.
example:
var numworkers = 4; var sharedworker = new worker("worker-shared.js"); for(var = 0; < numworkers; i++) { var dedicatedworker = new worker("worker-dedicated.js"); var channel = new messagechannel(); dedicatedworker.postmessage({sharedworkerport: channel.port1}, [channel.port1]); sharedworker.postmessage({workerport: channel.port2}, [channel.port2]); }
Comments
Post a Comment