Skip to content

Instantly share code, notes, and snippets.

@indutny

indutny/1.js Secret

Created January 14, 2013 10:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save indutny/79b5268e416d3c170ad8 to your computer and use it in GitHub Desktop.
Save indutny/79b5268e416d3c170ad8 to your computer and use it in GitHub Desktop.
var cp = require('child_process'),
net = require('net'),
http = require('http');
if (process.argv[2] === 'normal') {
var server = http.createServer(function(req, res) {
res.end('hello');
});
console.log("webServer started on " + process.pid);
process.on("message", function(msg,socket) {
process.nextTick(function(){
if(msg == 'c' && socket) {
socket.readable = socket.writable = true;
socket.resume();
server.connections++;
socket.server = server;
server.emit("connection", socket);
socket.emit("connect");
}
});
});
return;
}
// Server
var workers = [];
for (var i = 0; i < 4; i++) {
workers.push(cp.fork('1.js', ['normal']));
}
var round = 0;
net.createServer(function(s) {
s.pause();
var worker = workers.shift();
worker.send('c',s);
workers.push(worker);
}).listen(8080);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment