|
@ -32,23 +32,34 @@ function forEach(obj, fn) { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if (cluster.isWorker) { |
|
|
if (cluster.isWorker) { |
|
|
// Create a tcp server
|
|
|
// Create a tcp server. This will be used as cluster-shared-server and as an
|
|
|
// this will be used as cluster-shared-server
|
|
|
// alternative IPC channel.
|
|
|
// and as an alternativ IPC channel
|
|
|
|
|
|
var server = net.Server(); |
|
|
var server = net.Server(); |
|
|
server.on('connection', function(socket) { |
|
|
var socket, message; |
|
|
|
|
|
|
|
|
// Tell master using TCP socket that a message is received
|
|
|
function maybeReply() { |
|
|
process.on('message', function(message) { |
|
|
if (!socket || !message) return; |
|
|
|
|
|
|
|
|
|
|
|
// Tell master using TCP socket that a message is received.
|
|
|
socket.write(JSON.stringify({ |
|
|
socket.write(JSON.stringify({ |
|
|
code: 'received message', |
|
|
code: 'received message', |
|
|
echo: message |
|
|
echo: message |
|
|
})); |
|
|
})); |
|
|
}); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
server.on('connection', function(socket_) { |
|
|
|
|
|
socket = socket_; |
|
|
|
|
|
maybeReply(); |
|
|
|
|
|
|
|
|
|
|
|
// Send a message back over the IPC channel.
|
|
|
process.send('message from worker'); |
|
|
process.send('message from worker'); |
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
process.on('message', function(message_) { |
|
|
|
|
|
message = message_; |
|
|
|
|
|
maybeReply(); |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
server.listen(common.PORT, '127.0.0.1'); |
|
|
server.listen(common.PORT, '127.0.0.1'); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@ -93,8 +104,7 @@ else if (cluster.isMaster) { |
|
|
worker.on('listening', function() { |
|
|
worker.on('listening', function() { |
|
|
|
|
|
|
|
|
client = net.connect(common.PORT, function() { |
|
|
client = net.connect(common.PORT, function() { |
|
|
|
|
|
// Send message to worker.
|
|
|
//Send message to worker
|
|
|
|
|
|
worker.send('message from master'); |
|
|
worker.send('message from master'); |
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
@ -105,7 +115,7 @@ else if (cluster.isMaster) { |
|
|
if (data.code === 'received message') { |
|
|
if (data.code === 'received message') { |
|
|
check('worker', data.echo === 'message from master'); |
|
|
check('worker', data.echo === 'message from master'); |
|
|
} else { |
|
|
} else { |
|
|
throw new Error('worng TCP message recived: ' + data); |
|
|
throw new Error('wrong TCP message recived: ' + data); |
|
|
} |
|
|
} |
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
@ -117,7 +127,6 @@ else if (cluster.isMaster) { |
|
|
worker.on('exit', function() { |
|
|
worker.on('exit', function() { |
|
|
process.exit(0); |
|
|
process.exit(0); |
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
process.once('exit', function() { |
|
|
process.once('exit', function() { |
|
|