Answer the question
In order to leave comments, you need to log in
How to fork on server.on('connection'?
Good day, node lovers.
Please do not kick too much for the question - I'm still new to the node.
Actually task: to create each time child process (worker) at new connection to the server.
var dnode = require('dnode');
var net = require('net');
var cluster = require('cluster');
if (cluster.isMaster) {
cluster.fork();
} else {
var server = net.createServer(function (c) {
var d = dnode({
//some code here
})
c.pipe(d).pipe(c);
});
server.listen(5050);
server.on('connection', function(id) {
cluster.fork();
});
}
Answer the question
In order to leave comments, you need to log in
// Fork a new worker
cluster.fork = function(env) {
// This can only be called from the master.
assert(cluster.isMaster);
// Make sure that the master has been initialized
cluster.setupMaster();
return (new cluster.Worker(env));
};
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question