Created
February 7, 2010 02:46
-
-
Save tim-smart/297155 to your computer and use it in GitHub Desktop.
Node.js Web Workers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// The main node process | |
// Will output to shell: | |
// { | |
// "test": "test" | |
// } | |
// "bam" | |
var worker = require('./webworker'), | |
sys = require('sys'); | |
var testWorker = new worker.Worker(__dirname + '/usage_worker.js'); | |
testWorker.addListener('message', function(data) { | |
sys.puts(sys.inspect(data)); | |
}); | |
testWorker.addListener('error', function(data) { | |
sys.puts(sys.inspect(data)); | |
}); | |
setTimeout(function() { | |
testWorker.postMessage("bam"); | |
}, 2000); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// The worker node process | |
var worker = require('./webworker'); | |
var emitter = worker.workerOpenStdio(); | |
emitter.addListener('message', function(data) { | |
worker.postMessage(data); | |
}); | |
worker.postMessage({test: "test"}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// A Web Worker implementation for Node.js | |
var events = require('events'); | |
var Worker = exports.Worker = function(filepath) { | |
process.mixin(this, events.EventEmitter.prototype); | |
arguments = Array.prototype.slice.call(arguments); | |
try { | |
this._child = process.createChildProcess('node', arguments); | |
} catch (e) { | |
this.emit('error', 'webworker.Worker: Failed to create child process. ' + e.toString(), filepath, null); | |
return; | |
} | |
var self = this; | |
this._child.addListener('output', function(data) { | |
if (null === data) | |
self.emit.call(self, 'exit'); | |
try { | |
data = JSON.parse(data); | |
self.emit.call(self, 'message', data); | |
} catch (e) { | |
self.emit.call(self, 'error', 'webworker.onmessage: Failed due to invalid response. ' + e.toString(), filepath, null); | |
} | |
}); | |
this._child.addListener('error', function(data) { | |
if ('string' === typeof data) { | |
self.emit.call(self, 'error', data, filepath, null); | |
} else | |
self.emit.call(self, 'error', 'webworker.onerror: Failed due to invaild response. ', filepath, null); | |
}); | |
}; | |
Worker.prototype = { | |
postMessage: function(data) { | |
try { | |
data = JSON.stringify(data); | |
this._child.write(data); | |
} catch (e) { | |
this.emit('error', 'webworker.postMessage: Failed due to invalid data. ' + e.toString(), __filename, null); | |
} | |
}, | |
terminate: function() { | |
this._child.kill(); | |
} | |
}; | |
exports.postMessage = function(data) { | |
try { | |
data = JSON.stringify(data); | |
process.stdio.write(data); | |
} catch (e) { | |
process.stdio.writeError('webworker.postMessage: Failed due to invalid data. ' + e.toString()); | |
} | |
}; | |
exports.workerOpenStdio = function() { | |
process.stdio.open(); | |
var emitter = new events.EventEmitter(); | |
process.stdio.addListener('data', function(data) { | |
try { | |
data = JSON.parse(data); | |
emitter.emit('message', data); | |
} catch (e) { | |
emitter.emit('error', 'webworker.workerOpenStdio: Failed to parse incoming message.' + e.toString()); | |
} | |
}); | |
return emitter; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment