This is the code of my worker:
onmessage = function (event) {
postMessage({'data': event.data}, ['http://localhost:9000']);
};
And this is the code where I use the worker:
var worker = new Worker("/path/to/my/worker.js");
worker.onmessage = function (event) {
console.log("RECEIVED: ", event.data);
};
worker.onerror = function (err) {
console.log('ERROR: ', err)
};
worker.postMessage({'data': 'blabla', 'msg': 'Hi'});
But when the postMessage in the worker code is called, it gives me this error: "Failed to execute 'postMessage' on 'DedicatedWorkerGlobalScope': Value at index 0 does not have a transferable type."
I've tried also to serialize the object with this code:
onmessage = function (event) {
postMessage(JSON.stringify({'data': event.data}), ['http://localhost:9000']);
};
But nothing has changed.
------ EDIT ----------------------------------------------------------------
I tried the following code in the worker file:
function count() {
var i = 0;
self.onmessage = function (event) {
postMessage("Hello " + event.data);
};
for (i = 0; i < 5; i++) {
postMessage(i);
}
postMessage("Finished");
}
count();
The postMessage inside the onmessage function is the only one which gives me an error.