added example files to example/cluster/* tweaked readme style example
This commit is contained in:
parent
e297474e1c
commit
1d8fae31ec
6 changed files with 1359 additions and 67 deletions
94
README.md
94
README.md
|
@ -799,55 +799,77 @@ Writing explicit buffers, int types, doubles, floats etc. as well as big endian
|
|||
|
||||
```javascript
|
||||
|
||||
var fs = require('fs');
|
||||
var ipc = require('node-ipc');
|
||||
var cpuCount = require('os').cpus().length;
|
||||
var cluster = require('cluster');
|
||||
var socketPath = '/tmp/ipc.sock';
|
||||
|
||||
const fs = require('fs');
|
||||
const ipc=require('../../../node-ipc');
|
||||
const cpuCount = require('os').cpus().length;
|
||||
const cluster = require('cluster');
|
||||
const socketPath = '/tmp/ipc.sock';
|
||||
|
||||
ipc.config.unlink = false;
|
||||
|
||||
if (cluster.isMaster) {
|
||||
if (fs.existsSync(socketPath)) {
|
||||
fs.unlinkSync(socketPath);
|
||||
}
|
||||
if (fs.existsSync(socketPath)) {
|
||||
fs.unlinkSync(socketPath);
|
||||
}
|
||||
|
||||
for (var i = 0; i < cpuCount; i++) {
|
||||
cluster.fork();
|
||||
}
|
||||
for (let i = 0; i < cpuCount; i++) {
|
||||
cluster.fork();
|
||||
}
|
||||
}else{
|
||||
ipc.serve(
|
||||
socketPath,
|
||||
function() {
|
||||
ipc.server.on(
|
||||
'currentDate',
|
||||
function(data,socket) {
|
||||
console.log(`pid ${process.pid} got: `, data);
|
||||
}
|
||||
);
|
||||
}
|
||||
);
|
||||
|
||||
ipc.server.start();
|
||||
console.log(`pid ${process.pid} listening on ${socketPath}`);
|
||||
}
|
||||
|
||||
else {
|
||||
ipc.serve(socketPath, function() {
|
||||
ipc.server.on('message', function(data) {
|
||||
console.log('pid ' + process.pid + ' got: ' + data.message);
|
||||
});
|
||||
});
|
||||
|
||||
ipc.server.start();
|
||||
console.log('pid ' + process.pid + ' listening on ' + socketPath);
|
||||
}
|
||||
```
|
||||
```
|
||||
|
||||
##### Client
|
||||
|
||||
```javascript
|
||||
|
||||
var fs = require('fs');
|
||||
var ipc = require('node-ipc');
|
||||
const fs = require('fs');
|
||||
const ipc = require('../../node-ipc');
|
||||
|
||||
var socketPath = '/tmp/ipc.sock';
|
||||
const socketPath = '/tmp/ipc.sock';
|
||||
|
||||
//loop forever so you can see the pid of the cluster sever change in the logs
|
||||
setInterval(
|
||||
function() {
|
||||
ipc.connectTo(
|
||||
'world',
|
||||
socketPath,
|
||||
connecting
|
||||
);
|
||||
},
|
||||
2000
|
||||
);
|
||||
|
||||
function connecting(socket) {
|
||||
ipc.of.world.on(
|
||||
'connect',
|
||||
function() {
|
||||
ipc.of.world.emit(
|
||||
'currentDate',
|
||||
{
|
||||
message: new Date().toISOString()
|
||||
}
|
||||
);
|
||||
ipc.disconnect('world');
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
setInterval(function() {
|
||||
ipc.connectTo('world', socketPath, function(socket) {
|
||||
ipc.of.world.on('connect', function() {
|
||||
ipc.of.world.emit('message', {
|
||||
message: new Date().toISOString()
|
||||
});
|
||||
ipc.disconnect('world');
|
||||
});
|
||||
});
|
||||
}, 2000);
|
||||
```
|
||||
|
||||
#### Licensed under DBAD license
|
||||
|
|
31
example/clusterUnixSocket/cluster-client.js
Normal file
31
example/clusterUnixSocket/cluster-client.js
Normal file
|
@ -0,0 +1,31 @@
|
|||
const fs = require('fs');
|
||||
const ipc = require('../../node-ipc');
|
||||
|
||||
const socketPath = '/tmp/ipc.sock';
|
||||
|
||||
//loop forever so you can see the pid of the cluster sever change in the logs
|
||||
setInterval(
|
||||
function() {
|
||||
ipc.connectTo(
|
||||
'world',
|
||||
socketPath,
|
||||
connecting
|
||||
);
|
||||
},
|
||||
2000
|
||||
);
|
||||
|
||||
function connecting(socket) {
|
||||
ipc.of.world.on(
|
||||
'connect',
|
||||
function() {
|
||||
ipc.of.world.emit(
|
||||
'currentDate',
|
||||
{
|
||||
message: new Date().toISOString()
|
||||
}
|
||||
);
|
||||
ipc.disconnect('world');
|
||||
}
|
||||
);
|
||||
}
|
32
example/clusterUnixSocket/cluster-server.js
Normal file
32
example/clusterUnixSocket/cluster-server.js
Normal file
|
@ -0,0 +1,32 @@
|
|||
const fs = require('fs');
|
||||
const ipc=require('../../../node-ipc');
|
||||
const cpuCount = require('os').cpus().length;
|
||||
const cluster = require('cluster');
|
||||
const socketPath = '/tmp/ipc.sock';
|
||||
|
||||
ipc.config.unlink = false;
|
||||
|
||||
if (cluster.isMaster) {
|
||||
if (fs.existsSync(socketPath)) {
|
||||
fs.unlinkSync(socketPath);
|
||||
}
|
||||
|
||||
for (let i = 0; i < cpuCount; i++) {
|
||||
cluster.fork();
|
||||
}
|
||||
}else{
|
||||
ipc.serve(
|
||||
socketPath,
|
||||
function() {
|
||||
ipc.server.on(
|
||||
'currentDate',
|
||||
function(data,socket) {
|
||||
console.log(`pid ${process.pid} got: `, data);
|
||||
}
|
||||
);
|
||||
}
|
||||
);
|
||||
|
||||
ipc.server.start();
|
||||
console.log(`pid ${process.pid} listening on ${socketPath}`);
|
||||
}
|
1209
package-lock.json
generated
Normal file
1209
package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load diff
|
@ -15,10 +15,9 @@
|
|||
"js-queue": "2.0.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"codacy-coverage": "1.1.3",
|
||||
"codacy-coverage": "2.0.0",
|
||||
"jasmine": "2.4.1",
|
||||
"istanbul": "0.4.1",
|
||||
"codacy-coverage": "2.0.0",
|
||||
"node-cmd": "2.0.0"
|
||||
},
|
||||
"scripts": {
|
||||
|
@ -28,7 +27,7 @@
|
|||
"coverup": "cat ./spec/coverage/lcov.info | codacy-coverage"
|
||||
},
|
||||
"pre-commit": [
|
||||
"cover"
|
||||
"cover"
|
||||
],
|
||||
"keywords": [
|
||||
"IPC",
|
||||
|
|
|
@ -61,13 +61,11 @@ class IPC{
|
|||
}
|
||||
}
|
||||
|
||||
function log(){
|
||||
function log(...args){
|
||||
if(this.config.silent){
|
||||
return;
|
||||
}
|
||||
|
||||
const args=Array.prototype.slice.call(arguments);
|
||||
|
||||
for(let i=0, count=args.length; i<count; i++){
|
||||
if(typeof args[i] != 'object'){
|
||||
continue;
|
||||
|
@ -111,9 +109,9 @@ function serve(path,callback){
|
|||
}
|
||||
if(!path){
|
||||
this.log(
|
||||
'Server path not specified, so defaulting to'.notice,
|
||||
'ipc.config.socketRoot + ipc.config.appspace + ipc.config.id'.variable,
|
||||
(this.config.socketRoot+this.config.appspace+this.config.id).data
|
||||
'Server path not specified, so defaulting to',
|
||||
'ipc.config.socketRoot + ipc.config.appspace + ipc.config.id',
|
||||
this.config.socketRoot+this.config.appspace+this.config.id
|
||||
);
|
||||
path=this.config.socketRoot+this.config.appspace+this.config.id;
|
||||
}
|
||||
|
@ -153,9 +151,9 @@ function serveNet(host,port,UDPType,callback){
|
|||
}
|
||||
if(!host){
|
||||
this.log(
|
||||
'Server host not specified, so defaulting to'.notice,
|
||||
'ipc.config.networkHost'.variable,
|
||||
this.config.networkHost.data
|
||||
'Server host not specified, so defaulting to',
|
||||
'ipc.config.networkHost',
|
||||
this.config.networkHost
|
||||
);
|
||||
host=this.config.networkHost;
|
||||
}
|
||||
|
@ -178,8 +176,8 @@ function serveNet(host,port,UDPType,callback){
|
|||
}
|
||||
if(!port){
|
||||
this.log(
|
||||
'Server port not specified, so defaulting to'.notice,
|
||||
'ipc.config.networkPort'.variable,
|
||||
'Server port not specified, so defaulting to',
|
||||
'ipc.config.networkPort',
|
||||
this.config.networkPort
|
||||
);
|
||||
port=this.config.networkPort;
|
||||
|
@ -227,16 +225,16 @@ function connect(id,path,callback){
|
|||
|
||||
if(!id){
|
||||
this.log(
|
||||
'Service id required'.warn,
|
||||
'Requested service connection without specifying service id. Aborting connection attempt'.notice
|
||||
'Service id required',
|
||||
'Requested service connection without specifying service id. Aborting connection attempt'
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
if(!path){
|
||||
this.log(
|
||||
'Service path not specified, so defaulting to'.notice,
|
||||
'ipc.config.socketRoot + ipc.config.appspace + id'.variable,
|
||||
'Service path not specified, so defaulting to',
|
||||
'ipc.config.socketRoot + ipc.config.appspace + id',
|
||||
(this.config.socketRoot+this.config.appspace+id).data
|
||||
);
|
||||
path=this.config.socketRoot+this.config.appspace+id;
|
||||
|
@ -245,9 +243,9 @@ function connect(id,path,callback){
|
|||
if(this.of[id]){
|
||||
if(!this.of[id].socket.destroyed){
|
||||
this.log(
|
||||
'Already Connected to'.notice,
|
||||
id.variable,
|
||||
'- So executing success without connection'.notice
|
||||
'Already Connected to',
|
||||
id,
|
||||
'- So executing success without connection'
|
||||
);
|
||||
callback();
|
||||
return;
|
||||
|
@ -267,8 +265,8 @@ function connect(id,path,callback){
|
|||
function connectNet(id,host,port,callback){
|
||||
if(!id){
|
||||
this.log(
|
||||
'Service id required'.warn,
|
||||
'Requested service connection without specifying service id. Aborting connection attempt'.notice
|
||||
'Service id required',
|
||||
'Requested service connection without specifying service id. Aborting connection attempt'
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
@ -284,9 +282,9 @@ function connectNet(id,host,port,callback){
|
|||
}
|
||||
if(!host){
|
||||
this.log(
|
||||
'Server host not specified, so defaulting to'.notice,
|
||||
'ipc.config.networkHost'.variable,
|
||||
this.config.networkHost.data
|
||||
'Server host not specified, so defaulting to',
|
||||
'ipc.config.networkHost',
|
||||
this.config.networkHost
|
||||
);
|
||||
host=this.config.networkHost;
|
||||
}
|
||||
|
@ -297,8 +295,8 @@ function connectNet(id,host,port,callback){
|
|||
}
|
||||
if(!port){
|
||||
this.log(
|
||||
'Server port not specified, so defaulting to'.notice,
|
||||
'ipc.config.networkPort'.variable,
|
||||
'Server port not specified, so defaulting to',
|
||||
'ipc.config.networkPort',
|
||||
this.config.networkPort
|
||||
);
|
||||
port=this.config.networkPort;
|
||||
|
@ -314,10 +312,11 @@ function connectNet(id,host,port,callback){
|
|||
|
||||
if(this.of[id]){
|
||||
if(!this.of[id].socket.destroyed){
|
||||
|
||||
this.log(
|
||||
'Already Connected to'.notice,
|
||||
id.variable,
|
||||
'- So executing success without connection'.notice
|
||||
'Already Connected to',
|
||||
id,
|
||||
'- So executing success without connection'
|
||||
);
|
||||
callback();
|
||||
return;
|
||||
|
|
Loading…
Reference in a new issue