added example files to example/cluster/* tweaked readme style example

This commit is contained in:
Brandon Miller 2017-07-21 05:18:16 -07:00
parent e297474e1c
commit 1d8fae31ec
6 changed files with 1359 additions and 67 deletions

View file

@ -799,11 +799,11 @@ Writing explicit buffers, int types, doubles, floats etc. as well as big endian
```javascript ```javascript
var fs = require('fs'); const fs = require('fs');
var ipc = require('node-ipc'); const ipc=require('../../../node-ipc');
var cpuCount = require('os').cpus().length; const cpuCount = require('os').cpus().length;
var cluster = require('cluster'); const cluster = require('cluster');
var socketPath = '/tmp/ipc.sock'; const socketPath = '/tmp/ipc.sock';
ipc.config.unlink = false; ipc.config.unlink = false;
@ -812,42 +812,64 @@ Writing explicit buffers, int types, doubles, floats etc. as well as big endian
fs.unlinkSync(socketPath); fs.unlinkSync(socketPath);
} }
for (var i = 0; i < cpuCount; i++) { for (let i = 0; i < cpuCount; i++) {
cluster.fork(); cluster.fork();
} }
}else{
ipc.serve(
socketPath,
function() {
ipc.server.on(
'currentDate',
function(data,socket) {
console.log(`pid ${process.pid} got: `, data);
} }
);
else { }
ipc.serve(socketPath, function() { );
ipc.server.on('message', function(data) {
console.log('pid ' + process.pid + ' got: ' + data.message);
});
});
ipc.server.start(); ipc.server.start();
console.log('pid ' + process.pid + ' listening on ' + socketPath); console.log(`pid ${process.pid} listening on ${socketPath}`);
} }
``` ```
##### Client ##### Client
```javascript ```javascript
var fs = require('fs'); const fs = require('fs');
var ipc = require('node-ipc'); const ipc = require('../../node-ipc');
var socketPath = '/tmp/ipc.sock'; const socketPath = '/tmp/ipc.sock';
setInterval(function() { //loop forever so you can see the pid of the cluster sever change in the logs
ipc.connectTo('world', socketPath, function(socket) { setInterval(
ipc.of.world.on('connect', function() { function() {
ipc.of.world.emit('message', { ipc.connectTo(
'world',
socketPath,
connecting
);
},
2000
);
function connecting(socket) {
ipc.of.world.on(
'connect',
function() {
ipc.of.world.emit(
'currentDate',
{
message: new Date().toISOString() message: new Date().toISOString()
}); }
);
ipc.disconnect('world'); ipc.disconnect('world');
}); }
}); );
}, 2000); }
``` ```
#### Licensed under DBAD license #### Licensed under DBAD license

View 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');
}
);
}

View 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

File diff suppressed because it is too large Load diff

View file

@ -15,10 +15,9 @@
"js-queue": "2.0.0" "js-queue": "2.0.0"
}, },
"devDependencies": { "devDependencies": {
"codacy-coverage": "1.1.3", "codacy-coverage": "2.0.0",
"jasmine": "2.4.1", "jasmine": "2.4.1",
"istanbul": "0.4.1", "istanbul": "0.4.1",
"codacy-coverage": "2.0.0",
"node-cmd": "2.0.0" "node-cmd": "2.0.0"
}, },
"scripts": { "scripts": {

View file

@ -61,13 +61,11 @@ class IPC{
} }
} }
function log(){ function log(...args){
if(this.config.silent){ if(this.config.silent){
return; return;
} }
const args=Array.prototype.slice.call(arguments);
for(let i=0, count=args.length; i<count; i++){ for(let i=0, count=args.length; i<count; i++){
if(typeof args[i] != 'object'){ if(typeof args[i] != 'object'){
continue; continue;
@ -111,9 +109,9 @@ function serve(path,callback){
} }
if(!path){ if(!path){
this.log( this.log(
'Server path not specified, so defaulting to'.notice, 'Server path not specified, so defaulting to',
'ipc.config.socketRoot + ipc.config.appspace + ipc.config.id'.variable, 'ipc.config.socketRoot + ipc.config.appspace + ipc.config.id',
(this.config.socketRoot+this.config.appspace+this.config.id).data this.config.socketRoot+this.config.appspace+this.config.id
); );
path=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){ if(!host){
this.log( this.log(
'Server host not specified, so defaulting to'.notice, 'Server host not specified, so defaulting to',
'ipc.config.networkHost'.variable, 'ipc.config.networkHost',
this.config.networkHost.data this.config.networkHost
); );
host=this.config.networkHost; host=this.config.networkHost;
} }
@ -178,8 +176,8 @@ function serveNet(host,port,UDPType,callback){
} }
if(!port){ if(!port){
this.log( this.log(
'Server port not specified, so defaulting to'.notice, 'Server port not specified, so defaulting to',
'ipc.config.networkPort'.variable, 'ipc.config.networkPort',
this.config.networkPort this.config.networkPort
); );
port=this.config.networkPort; port=this.config.networkPort;
@ -227,16 +225,16 @@ function connect(id,path,callback){
if(!id){ if(!id){
this.log( this.log(
'Service id required'.warn, 'Service id required',
'Requested service connection without specifying service id. Aborting connection attempt'.notice 'Requested service connection without specifying service id. Aborting connection attempt'
); );
return; return;
} }
if(!path){ if(!path){
this.log( this.log(
'Service path not specified, so defaulting to'.notice, 'Service path not specified, so defaulting to',
'ipc.config.socketRoot + ipc.config.appspace + id'.variable, 'ipc.config.socketRoot + ipc.config.appspace + id',
(this.config.socketRoot+this.config.appspace+id).data (this.config.socketRoot+this.config.appspace+id).data
); );
path=this.config.socketRoot+this.config.appspace+id; 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]){
if(!this.of[id].socket.destroyed){ if(!this.of[id].socket.destroyed){
this.log( this.log(
'Already Connected to'.notice, 'Already Connected to',
id.variable, id,
'- So executing success without connection'.notice '- So executing success without connection'
); );
callback(); callback();
return; return;
@ -267,8 +265,8 @@ function connect(id,path,callback){
function connectNet(id,host,port,callback){ function connectNet(id,host,port,callback){
if(!id){ if(!id){
this.log( this.log(
'Service id required'.warn, 'Service id required',
'Requested service connection without specifying service id. Aborting connection attempt'.notice 'Requested service connection without specifying service id. Aborting connection attempt'
); );
return; return;
} }
@ -284,9 +282,9 @@ function connectNet(id,host,port,callback){
} }
if(!host){ if(!host){
this.log( this.log(
'Server host not specified, so defaulting to'.notice, 'Server host not specified, so defaulting to',
'ipc.config.networkHost'.variable, 'ipc.config.networkHost',
this.config.networkHost.data this.config.networkHost
); );
host=this.config.networkHost; host=this.config.networkHost;
} }
@ -297,8 +295,8 @@ function connectNet(id,host,port,callback){
} }
if(!port){ if(!port){
this.log( this.log(
'Server port not specified, so defaulting to'.notice, 'Server port not specified, so defaulting to',
'ipc.config.networkPort'.variable, 'ipc.config.networkPort',
this.config.networkPort this.config.networkPort
); );
port=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]){
if(!this.of[id].socket.destroyed){ if(!this.of[id].socket.destroyed){
this.log( this.log(
'Already Connected to'.notice, 'Already Connected to',
id.variable, id,
'- So executing success without connection'.notice '- So executing success without connection'
); );
callback(); callback();
return; return;