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')(); + + +
+