Skip to content

Instantly share code, notes, and snippets.

@indutny indutny/1.js Secret
Created Jan 14, 2013

Embed
What would you like to do?
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
You can’t perform that action at this time.