added ipc.disconnect method and multiple client example
This commit is contained in:
parent
bedf38a4ec
commit
677bf5fa12
14 changed files with 304 additions and 32 deletions
43
example/unixSocket/Multi-Client-Broadcast/goodbye-client.js
Normal file
43
example/unixSocket/Multi-Client-Broadcast/goodbye-client.js
Normal 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');
|
||||
}
|
||||
);
|
||||
}
|
||||
);
|
49
example/unixSocket/Multi-Client-Broadcast/hello-client.js
Normal file
49
example/unixSocket/Multi-Client-Broadcast/hello-client.js
Normal 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');
|
||||
}
|
||||
);
|
||||
}
|
||||
);
|
51
example/unixSocket/Multi-Client-Broadcast/world-server.js
Normal file
51
example/unixSocket/Multi-Client-Broadcast/world-server.js
Normal 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();
|
|
@ -18,8 +18,11 @@ ipc.connectTo(
|
|||
function(){
|
||||
ipc.log('## connected to world ##'.rainbow, ipc.config.delay);
|
||||
ipc.of.world.emit(
|
||||
'message',
|
||||
'hello'
|
||||
'app.message',
|
||||
{
|
||||
id : ipc.config.id,
|
||||
message : 'hello'
|
||||
}
|
||||
)
|
||||
}
|
||||
);
|
||||
|
@ -30,12 +33,10 @@ ipc.connectTo(
|
|||
}
|
||||
);
|
||||
ipc.of.world.on(
|
||||
'message',
|
||||
'app.message',
|
||||
function(data){
|
||||
ipc.log('got a message from world : '.debug, data);
|
||||
}
|
||||
);
|
||||
}
|
||||
);
|
||||
|
||||
console.log(ipc)
|
|
@ -13,19 +13,22 @@ ipc.config.retry= 1500;
|
|||
ipc.serve(
|
||||
function(){
|
||||
ipc.server.on(
|
||||
'message',
|
||||
'app.message',
|
||||
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(
|
||||
socket,
|
||||
'message',
|
||||
data+' world!'
|
||||
'app.message',
|
||||
{
|
||||
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();
|
||||
|
|
|
@ -33,6 +33,13 @@ function connect(){
|
|||
//init client object for scope persistance especially inside of socket events.
|
||||
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);
|
||||
if(!this.path){
|
||||
client.log('\n\n######\nerror: '.error, client.id .info,' client has not specified socket path it wishes to connect to.'.error);
|
||||
|
|
14
node-ipc.js
14
node-ipc.js
|
@ -33,6 +33,7 @@ var ipc = {
|
|||
config : defaults,
|
||||
connectTo : connect,
|
||||
connectToNet: connectNet,
|
||||
disconnect : disconnect,
|
||||
serve : serve,
|
||||
serveNet : serveNet,
|
||||
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){
|
||||
if(typeof path=='function'){
|
||||
callback=path;
|
||||
|
|
62
node_modules/event-pubsub/README.md
generated
vendored
62
node_modules/event-pubsub/README.md
generated
vendored
|
@ -4,9 +4,28 @@ Event PubSub
|
|||
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!
|
||||
|
||||
[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
|
||||
|
||||
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
|
||||
* **********************************/
|
||||
|
@ -33,10 +60,29 @@ Easy for any developer level. No frills, just high speed pubsub events!
|
|||
'world'
|
||||
);
|
||||
|
||||
events.trigger(
|
||||
'removeEvents'
|
||||
);
|
||||
|
||||
|
||||
#### Browser
|
||||
##### 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(
|
||||
'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
|
||||
* **********************************/
|
||||
|
@ -59,3 +113,7 @@ Easy for any developer level. No frills, just high speed pubsub events!
|
|||
'hello',
|
||||
'world'
|
||||
);
|
||||
|
||||
events.trigger(
|
||||
'removeEvents'
|
||||
);
|
||||
|
|
25
node_modules/event-pubsub/event-pubsub-browser.js
generated
vendored
25
node_modules/event-pubsub/event-pubsub-browser.js
generated
vendored
|
@ -13,6 +13,20 @@ window.pubsub=(
|
|||
function unsub(type,handler){
|
||||
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])
|
||||
return;
|
||||
|
||||
|
@ -21,13 +35,18 @@ window.pubsub=(
|
|||
return;
|
||||
}
|
||||
|
||||
if(this._events_[type].length<2){
|
||||
delete this._events_[type];
|
||||
return;
|
||||
}
|
||||
|
||||
for(var i=0,
|
||||
count=this._events_[type].length;
|
||||
i<count;
|
||||
i++){
|
||||
|
||||
i++
|
||||
){
|
||||
if(this._events_[type][i]==handler)
|
||||
delete this._events_[type][i];
|
||||
this._events_[type].splice(i,1);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
|
25
node_modules/event-pubsub/event-pubsub.js
generated
vendored
25
node_modules/event-pubsub/event-pubsub.js
generated
vendored
|
@ -10,6 +10,20 @@ function sub(type,handler){
|
|||
function unsub(type,handler){
|
||||
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])
|
||||
return;
|
||||
|
||||
|
@ -18,13 +32,18 @@ function unsub(type,handler){
|
|||
return;
|
||||
}
|
||||
|
||||
if(this._events_[type].length<2){
|
||||
delete this._events_[type];
|
||||
return;
|
||||
}
|
||||
|
||||
for(var i=0,
|
||||
count=this._events_[type].length;
|
||||
i<count;
|
||||
i++){
|
||||
|
||||
i++
|
||||
){
|
||||
if(this._events_[type][i]==handler)
|
||||
delete this._events_[type][i];
|
||||
this._events_[type].splice(i,1);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
|
2
node_modules/event-pubsub/examples/browser/basic.js
generated
vendored
2
node_modules/event-pubsub/examples/browser/basic.js
generated
vendored
|
@ -34,6 +34,8 @@ events.on(
|
|||
'world',
|
||||
function(data){
|
||||
eventLog.log('World event got',data);
|
||||
events.off('*');
|
||||
eventLog.log('Removed all events')
|
||||
}
|
||||
);
|
||||
|
||||
|
|
2
node_modules/event-pubsub/examples/node/basic.js
generated
vendored
2
node_modules/event-pubsub/examples/node/basic.js
generated
vendored
|
@ -34,6 +34,8 @@ events.on(
|
|||
'world',
|
||||
function(data){
|
||||
console.log('World event got',data);
|
||||
events.off('*');
|
||||
console.log('Removed all events');
|
||||
}
|
||||
);
|
||||
|
||||
|
|
14
node_modules/event-pubsub/package.json
generated
vendored
14
node_modules/event-pubsub/package.json
generated
vendored
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"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!",
|
||||
"main": "event-pubsub.js",
|
||||
"directories": {
|
||||
|
@ -27,9 +27,13 @@
|
|||
"bugs": {
|
||||
"url": "https://github.com/RIAEvangelist/event-pubsub/issues"
|
||||
},
|
||||
"homepage": "https://github.com/RIAEvangelist/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",
|
||||
"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[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",
|
||||
"_id": "event-pubsub@1.0.2",
|
||||
"_from": "event-pubsub@"
|
||||
"_id": "event-pubsub@1.0.3",
|
||||
"dist": {
|
||||
"shasum": "c81c49b101cdb4892d8fa2631b443184db2de6aa"
|
||||
},
|
||||
"_from": "event-pubsub@1.0.3",
|
||||
"_resolved": "https://registry.npmjs.org/event-pubsub/-/event-pubsub-1.0.3.tgz"
|
||||
}
|
||||
|
|
|
@ -1,13 +1,13 @@
|
|||
{
|
||||
"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.",
|
||||
"main": "node-ipc.js",
|
||||
"directories": {
|
||||
"example": "example"
|
||||
},
|
||||
"dependencies": {
|
||||
"event-pubsub": "~1.0.2",
|
||||
"event-pubsub": "~1.0.3",
|
||||
"colors": "~0.6.2"
|
||||
},
|
||||
"devDependencies": {},
|
||||
|
@ -42,5 +42,5 @@
|
|||
"bugs": {
|
||||
"url": "https://github.com/RIAEvangelist/node-ipc/issues"
|
||||
},
|
||||
"homepage": "riaevangelist.github.io/node-ipc/"
|
||||
"homepage": "http://riaevangelist.github.io/node-ipc/"
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue