diff --git a/examples/trivial/master.js b/examples/trivial/master.js new file mode 100644 index 0000000..97fc2a1 --- /dev/null +++ b/examples/trivial/master.js @@ -0,0 +1,8 @@ +var Worker = require("webworker").Worker; +var worker = new Worker(__dirname + "/worker.js"); +worker.onmessage = function (message) { + console.log(message.data); + worker.terminate(); +}; +worker.postMessage("Hello, World!"); + diff --git a/examples/trivial/worker.js b/examples/trivial/worker.js new file mode 100644 index 0000000..963c04c --- /dev/null +++ b/examples/trivial/worker.js @@ -0,0 +1,6 @@ +var worker = this; +worker.onmessage = function (message) { + // echo + worker.postMessage(message.data); +} + diff --git a/lib/webworker-child.js b/lib/webworker-child.js index 34b3c9f..29a77ba 100644 --- a/lib/webworker-child.js +++ b/lib/webworker-child.js @@ -125,7 +125,7 @@ ws.addListener('open', function() { process.addListener('uncaughtException', exceptionHandler); // Execute the worker - scriptObj.runInNewContext(workerCtx); + scriptObj.runInThisContext(); }); // Construt the Script object to host the worker's code @@ -145,7 +145,7 @@ default: } // Set up the context for the worker instance -var workerCtx = {}; +var workerCtx = global; // Context elements required for node.js //