diff --git a/example/unixSocket/Multi-Client-Broadcast/goodbye-client.js b/example/unixSocket/Multi-Client-Broadcast/goodbye-client.js new file mode 100644 index 0000000..77f922f --- /dev/null +++ b/example/unixSocket/Multi-Client-Broadcast/goodbye-client.js @@ -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'); + } + ); + } +); \ No newline at end of file diff --git a/example/unixSocket/Multi-Client-Broadcast/hello-client.js b/example/unixSocket/Multi-Client-Broadcast/hello-client.js new file mode 100644 index 0000000..00d70c3 --- /dev/null +++ b/example/unixSocket/Multi-Client-Broadcast/hello-client.js @@ -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'); + } + ); + } +); \ No newline at end of file diff --git a/example/unixSocket/Multi-Client-Broadcast/world-server.js b/example/unixSocket/Multi-Client-Broadcast/world-server.js new file mode 100644 index 0000000..b956925 --- /dev/null +++ b/example/unixSocket/Multi-Client-Broadcast/world-server.js @@ -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(); diff --git a/example/unixSocket/basic/hello-client.js b/example/unixSocket/basic/hello-client.js index 35640e8..15f67f0 100644 --- a/example/unixSocket/basic/hello-client.js +++ b/example/unixSocket/basic/hello-client.js @@ -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) \ No newline at end of file +); \ No newline at end of file diff --git a/example/unixSocket/basic/world-server.js b/example/unixSocket/basic/world-server.js index 8e695d3..cd57127 100644 --- a/example/unixSocket/basic/world-server.js +++ b/example/unixSocket/basic/world-server.js @@ -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(); diff --git a/lib/client.js b/lib/client.js index 0a9285f..33e9e0b 100644 --- a/lib/client.js +++ b/lib/client.js @@ -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); diff --git a/node-ipc.js b/node-ipc.js index 1545246..8439afb 100644 --- a/node-ipc.js +++ b/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; diff --git a/node_modules/event-pubsub/README.md b/node_modules/event-pubsub/README.md index b51ba1a..eb00009 100644 --- a/node_modules/event-pubsub/README.md +++ b/node_modules/event-pubsub/README.md @@ -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 * **********************************/ @@ -32,11 +59,30 @@ Easy for any developer level. No frills, just high speed pubsub events! 'hello', 'world' ); + + events.trigger( + 'removeEvents' + ); + #### Browser ##### HTML - var events = new require('../../event-pubsub.js')(); + + + + PubSub Example + + + + + ... + + + +##### 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' + ); diff --git a/node_modules/event-pubsub/event-pubsub-browser.js b/node_modules/event-pubsub/event-pubsub-browser.js index 9a02d9f..0b59a9e 100644 --- a/node_modules/event-pubsub/event-pubsub-browser.js +++ b/node_modules/event-pubsub/event-pubsub-browser.js @@ -12,7 +12,21 @@ 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\n \n \n PubSub Example\n \n \n \n \n ...\n \n \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" } diff --git a/package.json b/package.json index f9edd64..80efd3d 100644 --- a/package.json +++ b/package.json @@ -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/" }