Created
August 1, 2010 23:24
-
-
Save FrankGrimm/503897 to your computer and use it in GitHub Desktop.
Event passing for #node.js
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
/* | |
* eventpassing.js | |
* | |
* Monkey patch for passing events between EventEmitters | |
* Usage: simply require() this module | |
* | |
* @author Frank Grimm (http://frankgrimm.net) | |
* @version 0.1.3 | |
* | |
*/ | |
require('events').EventEmitter.prototype.pass = function(type, targetEmitter) { | |
// define passing handler to source EventEmitter | |
this.on(type, function() { | |
// changed to use splice (as in http://github.com/substack/stackvm/blob/dnode/lib/session.js#L17-L24 ) | |
var newArgs = [].slice.apply(arguments); | |
// unshift type argument | |
newArgs.unshift(type); | |
targetEmitter.emit.apply(targetEmitter, newArgs); | |
}); | |
// return this to allow concatenation | |
return this; | |
} |
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
// first of all, set up two HTTP servers | |
var http = require('http'); | |
// first server, w/o calling .listen() | |
var http1 = http.createServer(function(request, response) { | |
response.write('Hello from alternate reality.\n'); | |
response.end('Goodbye.\n'); | |
}); | |
// second (main) server w/ call to .listen() | |
var http0 = http.createServer() | |
.on('request', function(request, response) { | |
response.writeHead(200, {'Content-type': 'text/plain'}); | |
response.end('Hello Reality.\n'); | |
}) | |
.on('connection', function(stream) { | |
console.log("Main HTTP Server object got a connection."); | |
}); | |
http0.listen(3000); // listen on TCP port 3000 with http0 | |
/* Output: | |
Hello Reality. | |
*/ |
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
// first of all, set up two HTTP servers | |
var http = require('http'); | |
var http1 = http.createServer(function(request, response) { | |
response.write('Hello from alternate reality.\n'); | |
response.end('Goodbye.\n'); | |
}); | |
var http0 = http.createServer() | |
.on('request', function(request, response) { | |
response.writeHead(200, {'Content-type': 'text/plain'}); | |
// notice: don't end() yet | |
response.write('Hello Reality.\n'); | |
}) | |
.on('connection', function(stream) { | |
console.log("Main HTTP Server object got a connection."); | |
}); | |
// pass 'request' event to http1 | |
require('eventpassing'); | |
http0.pass('request', http1); | |
http0.listen(3000); // listen on TCP port 3000 with http0 | |
/* Output: | |
Hello Reality. | |
Hello from alternate reality. | |
Goodbye. | |
*/ |
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
var http = require('http'); | |
var http1 = http.createServer(function(request, response) { | |
// this is now called first | |
response.writeHead(200, {'Content-type': 'text/plain'}); | |
response.write('Hello from alternate reality.\n'); | |
}).on('connection', function(stream) { | |
console.log("New connection from: " + stream.remoteAddress); | |
}); | |
var http0 = http.createServer() | |
.on('request', function(request, response) { | |
// end here because this is the last 'request'-Handler called | |
response.end('Goodbye from Reality.\n'); | |
}) | |
.on('connection', function(stream) { | |
console.log("Main HTTP Server object got a connection."); | |
}); | |
// pass 'request' event to http1 | |
require('eventpassing'); | |
//http0.pass('request', http1); not necessary anymore | |
http0.pass('connection', http1); | |
// pass requests in http1 back to original http server | |
http1.pass('request', http0); | |
http0.listen(3000); // listen on TCP port 3000 with http0 | |
/* Output: | |
Hello from alternate reality. | |
Goodbye from Reality. | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment