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'),
pubsub = require('event-pubsub'),
Message = require('js-message'),
fs = require('fs');
fs = require('fs'),
Queue = require('js-queue');
function init(config,log){
var client={
config : config,
queue : new Queue,
socket : false,
connect : connect,
emit : emit,
@ -32,9 +34,21 @@ function emit(type,data){
message=eventParser.format(message);
}
this.socket.write(message);
if(!this.config.sync){
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(){
//init client object for scope persistance especially inside of socket events.
var client=this;
@ -176,6 +190,11 @@ function connect(){
'data',
new Buffer(data,this.encoding)
);
if(!client.config.sync){
return;
}
client.queue.next();
return;
}
@ -203,6 +222,12 @@ function connect(){
message.data
);
}
if(!client.config.sync){
return;
}
client.queue.next();
}
);
}

View File

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

View File

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

View File

@ -6,13 +6,14 @@
"directories": {
"example": "example"
},
"engines" : {
"node" : ">=1.0.0"
"engines": {
"node": ">=1.0.0"
},
"dependencies": {
"colors": "*",
"event-pubsub": "*",
"js-message": "*",
"js-queue": "^0.1.2",
"node-cmd": "*"
},
"devDependencies": {},