Created
July 7, 2014 13:51
-
-
Save samcday/d20f91aae0e4dbad27e8 to your computer and use it in GitHub Desktop.
Node cluster is gone crazy!
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 net = require("net"); | |
var target = 10; | |
var created = 0; | |
function createConnection() { | |
if (created === target) { | |
return; | |
} | |
var id = created++; | |
var conn = net.createConnection({port: 3000}) | |
conn.write("01234567890"); | |
conn.setEncoding("utf8"); | |
var data = ""; | |
conn.on("data", function(chunk) { | |
data += chunk; | |
}); | |
conn.on("error", function(err) { | |
console.log("ERROR :(", err); | |
}); | |
conn.on("end", function() { | |
console.log(id + ": " + data); | |
}) | |
setImmediate(createConnection); | |
} | |
createConnection(); |
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 cluster = require("cluster"); | |
if (cluster.isMaster) { | |
cluster.fork().process.on("message", function(msg, connection) { | |
connection.once("readable", function() { | |
var data = connection.read(); | |
connection.write("I read: " + data); | |
connection.end(); | |
}); | |
}); | |
} | |
else | |
{ | |
var server = require("net").createServer(); | |
server.on("connection", function(c) { | |
process.send("conn", c); | |
}); | |
server.listen(3000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment