added ipc.disconnect method and multiple client example

This commit is contained in:
Brandon Miller 2014-03-01 04:13:06 -08:00
parent bedf38a4ec
commit 677bf5fa12
14 changed files with 304 additions and 32 deletions

View file

@ -0,0 +1,43 @@
var ipc=require('../../../node-ipc');
/***************************************\
*
* You should start both hello and world
* then you will see them communicating.
*
* *************************************/
ipc.config.id = 'goodbye';
ipc.config.retry= 1500;
ipc.connectTo(
'world',
function(){
ipc.of.world.on(
'connect',
function(){
ipc.log('## connected to world ##'.rainbow, ipc.config.delay);
ipc.of.world.emit(
'app.message',
{
id : ipc.config.id,
message : 'goodbye'
}
)
}
);
ipc.of.world.on(
'disconnect',
function(){
ipc.log('disconnected from world'.notice);
}
);
ipc.of.world.on(
'kill.connection',
function(data){
ipc.log('world requested kill.connection'.notice);
ipc.disconnect('world');
}
);
}
);

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= 1500;
ipc.connectTo(
'world',
function(){
ipc.of.world.on(
'connect',
function(){
ipc.log('## connected to world ##'.rainbow, ipc.config.delay);
ipc.of.world.emit(
'app.message',
{
id : ipc.config.id,
message : 'hello'
}
)
}
);
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);
}
);
ipc.of.world.on(
'kill.connection',
function(data){
ipc.log('world requested kill.connection'.notice);
ipc.disconnect('world');
}
);
}
);

View file

@ -0,0 +1,51 @@
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;
var messages={
goodbye:false,
hello:false
}
ipc.serve(
function(){
ipc.server.on(
'app.message',
function(data,socket){
ipc.log('got a message from'.debug, (data.id).variable, (data.message).data);
messages[data.id]=true;
ipc.server.emit(
socket,
'app.message',
{
id : ipc.config.id,
message : data.message+' world!'
}
);
if(messages.hello && messages.goodbye){
ipc.log('got all required events, telling clients to kill connection'.good);
ipc.server.broadcast(
'kill.connection',
{
id:ipc.config.id
}
);
}
}
);
}
);
ipc.server.define.listen['app.message']='This event type listens for message strings as value of data key.';
ipc.server.define.broadcast['kill.connection']='This event is a command to kill connection to this server, the data object will contain the id of this server incase the client needs it';
ipc.server.start();

View file

@ -18,8 +18,11 @@ ipc.connectTo(
function(){ function(){
ipc.log('## connected to world ##'.rainbow, ipc.config.delay); ipc.log('## connected to world ##'.rainbow, ipc.config.delay);
ipc.of.world.emit( ipc.of.world.emit(
'message', 'app.message',
'hello' {
id : ipc.config.id,
message : 'hello'
}
) )
} }
); );
@ -30,12 +33,10 @@ ipc.connectTo(
} }
); );
ipc.of.world.on( ipc.of.world.on(
'message', 'app.message',
function(data){ function(data){
ipc.log('got a message from world : '.debug, data); ipc.log('got a message from world : '.debug, data);
} }
); );
} }
); );
console.log(ipc)

View file

@ -13,19 +13,22 @@ ipc.config.retry= 1500;
ipc.serve( ipc.serve(
function(){ function(){
ipc.server.on( ipc.server.on(
'message', 'app.message',
function(data,socket){ function(data,socket){
ipc.log('got a message : '.debug, data); ipc.log('got a message from'.debug, (data.id).variable, (data.message).data);
ipc.server.emit( ipc.server.emit(
socket, socket,
'message', 'app.message',
data+' world!' {
id : ipc.config.id,
message : data.message+' world!'
}
); );
} }
); );
} }
); );
ipc.server.define.listen.message='This event type listens for message strings as value of data key.'; ipc.server.define.listen['app.message']='This event type listens for message strings as value of data key.';
ipc.server.start(); ipc.server.start();

View file

@ -33,6 +33,13 @@ 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;
if(client.socket){
if(client.socket.destroyed){
client.log('Requested to connect or reconnect to a destroyed socket. Not attempting because socket distroyed'.notice);
return;
}
}
client.log('requested connection to '.debug, client.id.variable, client.path.variable); client.log('requested connection to '.debug, client.id.variable, client.path.variable);
if(!this.path){ if(!this.path){
client.log('\n\n######\nerror: '.error, client.id .info,' client has not specified socket path it wishes to connect to.'.error); client.log('\n\n######\nerror: '.error, client.id .info,' client has not specified socket path it wishes to connect to.'.error);

View file

@ -33,6 +33,7 @@ var ipc = {
config : defaults, config : defaults,
connectTo : connect, connectTo : connect,
connectToNet: connectNet, connectToNet: connectNet,
disconnect : disconnect,
serve : serve, serve : serve,
serveNet : serveNet, serveNet : serveNet,
of : {}, of : {},
@ -49,6 +50,19 @@ function log(){
); );
} }
function disconnect(id){
if(!ipc.of[id])
return;
ipc.of[id].off('*');
if(ipc.of[id].socket){
if(ipc.of[id].socket.destroy)
ipc.of[id].socket.destroy();
}
delete ipc.of[id];
}
function serve(path,callback){ function serve(path,callback){
if(typeof path=='function'){ if(typeof path=='function'){
callback=path; callback=path;

62
node_modules/event-pubsub/README.md generated vendored
View file

@ -4,9 +4,28 @@ Event PubSub
Pubsub events for Node and the browser allowing event scoping and multiple scopes. Pubsub events for Node and the browser allowing event scoping and multiple scopes.
Easy for any developer level. No frills, just high speed pubsub events! Easy for any developer level. No frills, just high speed pubsub events!
[Pretty GitHub.io site](http://riaevangelist.github.io/event-pubsub/)
[![alt event-pubsub npm details](https://nodei.co/npm/event-pubsub.png?stars=true "event-pubsub npm package details")](https://npmjs.org/package/event-pubsub)
**EXAMPLE FILES**
1. [Node Pubsub Event Examples](https://github.com/RIAEvangelist/event-pubsub/tree/master/examples/node)
2. [Browser Pubsub Event Examples](https://github.com/RIAEvangelist/event-pubsub/tree/master/examples/browser)
**Node Install**
``npm install event-pubsub``
**Browser Install**
*see browser examples above or below*
--- ---
### Basic Examples ### Basic Example
--- ---
***NOTE - the only diffeence between node and browser code is how the ``events`` variable is created***
* node ``var events = new require('../../event-pubsub.js')();``
* browser ``var events = new window.pubsub();``
#### Node #### Node
var events = new require('../../event-pubsub.js')(); var events = new require('../../event-pubsub.js')();
@ -25,6 +44,14 @@ Easy for any developer level. No frills, just high speed pubsub events!
} }
); );
events.on(
'removeEvents',
function(){
events.off('*');
console.log('Removed all events');
}
);
/************************************\ /************************************\
* trigger events for testing * trigger events for testing
* **********************************/ * **********************************/
@ -32,11 +59,30 @@ Easy for any developer level. No frills, just high speed pubsub events!
'hello', 'hello',
'world' 'world'
); );
events.trigger(
'removeEvents'
);
#### Browser #### Browser
##### HTML ##### HTML
var events = new require('../../event-pubsub.js')(); <!DOCTYPE html>
<html>
<head>
<title>PubSub Example</title>
<script src='../../event-pubsub-browser.js'></script>
<script src='yourAmazingCode.js'></script>
</head>
<body>
...
</body>
</html>
##### Inside Your Amazing Code
var events = new window.pubsub();
events.on( events.on(
'hello', 'hello',
@ -52,6 +98,14 @@ Easy for any developer level. No frills, just high speed pubsub events!
} }
); );
events.on(
'removeEvents',
function(){
events.off('*');
console.log('Removed all events');
}
);
/************************************\ /************************************\
* trigger events for testing * trigger events for testing
* **********************************/ * **********************************/
@ -59,3 +113,7 @@ Easy for any developer level. No frills, just high speed pubsub events!
'hello', 'hello',
'world' 'world'
); );
events.trigger(
'removeEvents'
);

View file

@ -12,7 +12,21 @@ window.pubsub=(
function unsub(type,handler){ function unsub(type,handler){
checkScope.apply(this); checkScope.apply(this);
if(type=='*'){
var params=Array.prototype.slice.call(arguments,1);
for(
var keys = Object.keys(this._events_),
count = keys.length,
i=0;
i<count;
i++
){
var args=params.unshift(keys[i]);
this.off.call(args);
}
}
if(!this._events_[type]) if(!this._events_[type])
return; return;
@ -21,13 +35,18 @@ window.pubsub=(
return; return;
} }
if(this._events_[type].length<2){
delete this._events_[type];
return;
}
for(var i=0, for(var i=0,
count=this._events_[type].length; count=this._events_[type].length;
i<count; i<count;
i++){ i++
){
if(this._events_[type][i]==handler) if(this._events_[type][i]==handler)
delete this._events_[type][i]; this._events_[type].splice(i,1);
return; return;
} }
} }

View file

@ -10,6 +10,20 @@ function sub(type,handler){
function unsub(type,handler){ function unsub(type,handler){
checkScope.apply(this); checkScope.apply(this);
if(type=='*'){
var params=Array.prototype.slice.call(arguments,1);
for(
var keys = Object.keys(this._events_),
count = keys.length,
i=0;
i<count;
i++
){
var args=params.unshift(keys[i]);
this.off.call(args);
}
}
if(!this._events_[type]) if(!this._events_[type])
return; return;
@ -18,13 +32,18 @@ function unsub(type,handler){
return; return;
} }
if(this._events_[type].length<2){
delete this._events_[type];
return;
}
for(var i=0, for(var i=0,
count=this._events_[type].length; count=this._events_[type].length;
i<count; i<count;
i++){ i++
){
if(this._events_[type][i]==handler) if(this._events_[type][i]==handler)
delete this._events_[type][i]; this._events_[type].splice(i,1);
return; return;
} }
} }

View file

@ -34,6 +34,8 @@ events.on(
'world', 'world',
function(data){ function(data){
eventLog.log('World event got',data); eventLog.log('World event got',data);
events.off('*');
eventLog.log('Removed all events')
} }
); );

View file

@ -34,6 +34,8 @@ events.on(
'world', 'world',
function(data){ function(data){
console.log('World event got',data); console.log('World event got',data);
events.off('*');
console.log('Removed all events');
} }
); );

View file

@ -1,6 +1,6 @@
{ {
"name": "event-pubsub", "name": "event-pubsub",
"version": "1.0.2", "version": "1.0.3",
"description": "Pubsub events for Node and the browser allowing event scoping and multiple scopes. Easy for any developer level. No frills, just high speed pubsub events!", "description": "Pubsub events for Node and the browser allowing event scoping and multiple scopes. Easy for any developer level. No frills, just high speed pubsub events!",
"main": "event-pubsub.js", "main": "event-pubsub.js",
"directories": { "directories": {
@ -27,9 +27,13 @@
"bugs": { "bugs": {
"url": "https://github.com/RIAEvangelist/event-pubsub/issues" "url": "https://github.com/RIAEvangelist/event-pubsub/issues"
}, },
"homepage": "https://github.com/RIAEvangelist/event-pubsub", "homepage": "http://riaevangelist.github.io/event-pubsub/",
"readme": "Event PubSub\n============\n\nPubsub events for Node and the browser allowing event scoping and multiple scopes. \nEasy for any developer level. No frills, just high speed pubsub events!\n\n---\n### Basic Examples\n---\n#### Node\n\n var events = new require('../../event-pubsub.js')();\n\n events.on(\n 'hello',\n function(data){\n console.log('hello event recieved ', data);\n }\n );\n \n events.on(\n '*',\n function(type){\n console.log('Catch all detected event type of : ',type, '. List of all the sent arguments ',arguments);\n }\n );\n \n /************************************\\\n * trigger events for testing\n * **********************************/\n events.trigger(\n 'hello',\n 'world'\n );\n\n#### Browser\n##### HTML\n\n var events = new require('../../event-pubsub.js')();\n\n events.on(\n 'hello',\n function(data){\n console.log('hello event recieved ', data);\n }\n );\n \n events.on(\n '*',\n function(type){\n console.log('Catch all detected event type of : ',type, '. List of all the sent arguments ',arguments);\n }\n );\n \n /************************************\\\n * trigger events for testing\n * **********************************/\n events.trigger(\n 'hello',\n 'world'\n );\n", "readme": "Event PubSub\n============\n\nPubsub events for Node and the browser allowing event scoping and multiple scopes. \nEasy for any developer level. No frills, just high speed pubsub events!\n\n[Pretty GitHub.io site](http://riaevangelist.github.io/event-pubsub/) \n\n[![alt event-pubsub npm details](https://nodei.co/npm/event-pubsub.png?stars=true \"event-pubsub npm package details\")](https://npmjs.org/package/event-pubsub)\n\n**EXAMPLE FILES** \n\n1. [Node Pubsub Event Examples](https://github.com/RIAEvangelist/event-pubsub/tree/master/examples/node) \n2. [Browser Pubsub Event Examples](https://github.com/RIAEvangelist/event-pubsub/tree/master/examples/browser)\n\n**Node Install** \n``npm install event-pubsub``\n\n**Browser Install** \n*see browser examples above or below*\n\n---\n### Basic Example\n---\n***NOTE - the only diffeence between node and browser code is how the ``events`` variable is created*** \n* node ``var events = new require('../../event-pubsub.js')();``\n* browser ``var events = new window.pubsub();``\n\n#### Node\n\n var events = new require('../../event-pubsub.js')();\n\n events.on(\n 'hello',\n function(data){\n console.log('hello event recieved ', data);\n }\n );\n \n events.on(\n '*',\n function(type){\n console.log('Catch all detected event type of : ',type, '. List of all the sent arguments ',arguments);\n }\n );\n \n events.on(\n 'removeEvents',\n function(){\n events.off('*');\n console.log('Removed all events');\n }\n );\n \n /************************************\\\n * trigger events for testing\n * **********************************/\n events.trigger(\n 'hello',\n 'world'\n );\n \n events.trigger(\n 'removeEvents'\n );\n \n\n#### Browser\n##### HTML\n\n <!DOCTYPE html>\n <html>\n <head>\n <title>PubSub Example</title>\n <script src='../../event-pubsub-browser.js'></script>\n <script src='yourAmazingCode.js'></script>\n </head>\n <body>\n ...\n </body>\n </html>\n\n##### Inside Your Amazing Code\n\n var events = new window.pubsub();\n\n events.on(\n 'hello',\n function(data){\n console.log('hello event recieved ', data);\n }\n );\n \n events.on(\n '*',\n function(type){\n console.log('Catch all detected event type of : ',type, '. List of all the sent arguments ',arguments);\n }\n );\n \n events.on(\n 'removeEvents',\n function(){\n events.off('*');\n console.log('Removed all events');\n }\n );\n \n /************************************\\\n * trigger events for testing\n * **********************************/\n events.trigger(\n 'hello',\n 'world'\n );\n \n events.trigger(\n 'removeEvents'\n );\n",
"readmeFilename": "README.md", "readmeFilename": "README.md",
"_id": "event-pubsub@1.0.2", "_id": "event-pubsub@1.0.3",
"_from": "event-pubsub@" "dist": {
"shasum": "c81c49b101cdb4892d8fa2631b443184db2de6aa"
},
"_from": "event-pubsub@1.0.3",
"_resolved": "https://registry.npmjs.org/event-pubsub/-/event-pubsub-1.0.3.tgz"
} }

View file

@ -1,13 +1,13 @@
{ {
"name": "node-ipc", "name": "node-ipc",
"version": "0.9.7", "version": "0.9.9",
"description": "A nodejs module for local and remote Inter Process Communication (IPC), Neural Networking, and able to facilitate machine learning.", "description": "A nodejs module for local and remote Inter Process Communication (IPC), Neural Networking, and able to facilitate machine learning.",
"main": "node-ipc.js", "main": "node-ipc.js",
"directories": { "directories": {
"example": "example" "example": "example"
}, },
"dependencies": { "dependencies": {
"event-pubsub": "~1.0.2", "event-pubsub": "~1.0.3",
"colors": "~0.6.2" "colors": "~0.6.2"
}, },
"devDependencies": {}, "devDependencies": {},
@ -42,5 +42,5 @@
"bugs": { "bugs": {
"url": "https://github.com/RIAEvangelist/node-ipc/issues" "url": "https://github.com/RIAEvangelist/node-ipc/issues"
}, },
"homepage": "riaevangelist.github.io/node-ipc/" "homepage": "http://riaevangelist.github.io/node-ipc/"
} }