added sync option for all applicable socket types

This commit is contained in:
Brandon Nozaki Miller 2015-12-10 00:11:14 -08:00
parent 0220080d54
commit 3be9d29240
10 changed files with 311 additions and 4 deletions

View file

@ -0,0 +1,46 @@
var ipc=require('../../../node-ipc');
/***************************************\
*
* You should start both hello and world
* then you will see them communicating.
*
* *************************************/
ipc.config.id = 'hello';
ipc.config.retry= 1500;
ipc.config.sync= true;
ipc.connectToNet(
'world',
function(){
ipc.of.world.on(
'connect',
function(){
ipc.log('## connected to world ##'.rainbow, ipc.config.delay);
//queue up a bunch of requests to be sent synchronously
for(var i=0; i<10; i++){
ipc.of.world.emit(
'message',
'hello'+i
)
}
}
);
ipc.of.world.on(
'disconnect',
function(){
ipc.log('disconnected from world'.notice);
}
);
ipc.of.world.on(
'message',
function(data){
ipc.log('got a message from world : '.debug, data,'\n\n');
}
);
}
);
console.log(ipc)

View file

@ -0,0 +1,45 @@
var ipc=require('../../../node-ipc');
/***************************************\
*
* You should start both hello and world
* then you will see them communicating.
*
* *************************************/
ipc.config.id = 'world';
ipc.config.retry= 1500;
ipc.config.sync = true; //sync servers do not bradcast defined events
ipc.serveNet(
function(){
ipc.server.on(
'message',
function(data,socket){
ipc.log('got a message : '.debug, data);
//fake some synch procedural code
setTimeout(
function(){
ipc.server.emit(
socket,
'message',
data+' world!'
);
},
3000
);
}
);
ipc.server.on(
'socket.disconnected',
function(data,socket){
console.log(arguments)
}
);
}
);
ipc.server.define.listen.message='This event type listens for message strings as value of data key.';
ipc.server.start();

View file

@ -0,0 +1,47 @@
var ipc=require('../../../node-ipc');
/***************************************\
*
* You should start both hello and world
* then you will see them communicating.
*
* *************************************/
ipc.config.id = 'hello';
ipc.config.retry= 1500;
ipc.config.sync= true;
ipc.config.tls={
rejectUnauthorized:false
};
ipc.connectToNet(
'world',
function(){
ipc.of.world.on(
'connect',
function(){
ipc.log('## connected to world ##'.rainbow, ipc.config.delay);
//queue up a bunch of requests to be sent synchronously
for(var i=0; i<10; i++){
ipc.of.world.emit(
'message',
'hello'+i
)
}
}
);
ipc.of.world.on(
'disconnect',
function(){
ipc.log('disconnected from world'.notice);
}
);
ipc.of.world.on(
'message',
function(data){
ipc.log('got a message from world : '.debug, data);
}
);
}
);

View file

@ -0,0 +1,48 @@
var ipc=require('../../../node-ipc');
/***************************************\
*
* You should start both hello and world
* then you will see them communicating.
*
* *************************************/
ipc.config.id = 'world';
ipc.config.retry= 1500;
ipc.config.sync= true;
ipc.config.tls={
public: '../../../local-node-ipc-certs/server.pub',
private: '../../../local-node-ipc-certs/private/server.key'
}
ipc.serveNet(
function(){
ipc.server.on(
'message',
function(data,socket){
ipc.log('got a message : '.debug, data);
setTimeout(
function(){
ipc.server.emit(
socket,
'message',
data+' world!'
);
},
3000
);
}
);
ipc.server.on(
'socket.disconnected',
function(data,socket){
console.log(arguments)
}
);
}
);
ipc.server.define.listen.message='This event type listens for message strings as value of data key.';
ipc.server.start();

View file

@ -0,0 +1,49 @@
var ipc=require('../../../node-ipc');
/***************************************\
*
* You should start both hello and world
* then you will see them communicating.
*
* *************************************/
ipc.config.id = 'hello';
ipc.config.retry = 1000;
ipc.config.sync= true;
ipc.connectTo(
'world',
function(){
ipc.of.world.on(
'connect',
function(){
ipc.log('## connected to world ##'.rainbow, ipc.config.delay);
//queue up a bunch of requests to be sent synchronously
for(var i=0; i<10; i++){
ipc.of.world.emit(
'app.message',
{
id : ipc.config.id,
message : 'hello'+i
}
)
}
}
);
ipc.of.world.on(
'disconnect',
function(){
ipc.log('disconnected from world'.notice);
}
);
ipc.of.world.on(
'app.message',
function(data){
ipc.log('got a message from world : '.debug, data);
}
);
console.log(ipc.of.world.destroy);
}
);

View file

@ -0,0 +1,41 @@
var ipc=require('../../../node-ipc');
/***************************************\
*
* You should start both hello and world
* then you will see them communicating.
*
* *************************************/
ipc.config.id = 'world';
ipc.config.retry= 1500;
ipc.config.sync= true;
ipc.serve(
function(){
ipc.server.on(
'app.message',
function(data,socket){
//ipc.log('got a message from'.debug, (data.id).variable, (data.message).data);
setTimeout(
function(){
ipc.server.emit(
socket,
'app.message',
{
id : ipc.config.id,
message : data.message+' world!'
}
);
},
2000
);
}
);
}
);
ipc.server.define.listen['app.message']='This event type listens for message strings as value of data key.';
ipc.server.start();

View file

@ -3,11 +3,13 @@ var net = require('net'),
eventParser = require('../lib/eventParser.js'), eventParser = require('../lib/eventParser.js'),
pubsub = require('event-pubsub'), pubsub = require('event-pubsub'),
Message = require('js-message'), Message = require('js-message'),
fs = require('fs'); fs = require('fs'),
Queue = require('js-queue');
function init(config,log){ function init(config,log){
var client={ var client={
config : config, config : config,
queue : new Queue,
socket : false, socket : false,
connect : connect, connect : connect,
emit : emit, emit : emit,
@ -32,9 +34,21 @@ function emit(type,data){
message=eventParser.format(message); message=eventParser.format(message);
} }
if(!this.config.sync){
this.socket.write(message); this.socket.write(message);
return;
}
this.queue.add(
syncEmit.bind(this,message)
);
}; };
function syncEmit(message){
this.log('dispatching event to '.debug, this.id.variable, this.path.variable,' : ', message.data);
this.socket.write(message);
}
function connect(){ function connect(){
//init client object for scope persistance especially inside of socket events. //init client object for scope persistance especially inside of socket events.
var client=this; var client=this;
@ -176,6 +190,11 @@ function connect(){
'data', 'data',
new Buffer(data,this.encoding) new Buffer(data,this.encoding)
); );
if(!client.config.sync){
return;
}
client.queue.next();
return; return;
} }
@ -203,6 +222,12 @@ function connect(){
message.data message.data
); );
} }
if(!client.config.sync){
return;
}
client.queue.next();
} }
); );
} }

View file

@ -267,6 +267,10 @@ function init(path,config,log,port){
return; return;
} }
if(server.config.sync){
return;
}
server.trigger( server.trigger(
'get.events.broadcasting', 'get.events.broadcasting',
socket socket

View file

@ -31,6 +31,7 @@ var defaults={
id : os.hostname(), id : os.hostname(),
encoding : 'utf8', encoding : 'utf8',
rawBuffer : false, rawBuffer : false,
sync : false,
silent : false, silent : false,
maxConnections : 100, maxConnections : 100,
retry : 500, retry : 500,

View file

@ -13,6 +13,7 @@
"colors": "*", "colors": "*",
"event-pubsub": "*", "event-pubsub": "*",
"js-message": "*", "js-message": "*",
"js-queue": "^0.1.2",
"node-cmd": "*" "node-cmd": "*"
}, },
"devDependencies": {}, "devDependencies": {},