added js-message dep and rawBuffer examples
This commit is contained in:
parent
7f06fe6a14
commit
ac714e91a4
14 changed files with 313 additions and 154 deletions
|
@ -36,7 +36,7 @@ ipc.connectToNet(
|
|||
ipc.of.world.on(
|
||||
'app.message',
|
||||
function(data){
|
||||
ipc.log('got a message from world : '.debug, data.messgae);
|
||||
ipc.log('got a message from world : '.debug, data.message);
|
||||
}
|
||||
);
|
||||
ipc.of.world.on(
|
||||
|
|
35
example/TCPSocket/rawBuffer/hello-client.js
Normal file
35
example/TCPSocket/rawBuffer/hello-client.js
Normal file
|
@ -0,0 +1,35 @@
|
|||
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.rawBuffer=true;
|
||||
ipc.config.encoding='ascii';
|
||||
|
||||
ipc.connectToNet(
|
||||
'world',
|
||||
function(){
|
||||
ipc.of.world.on(
|
||||
'connect',
|
||||
function(){
|
||||
ipc.log('## connected to world ##'.rainbow, ipc.config.delay);
|
||||
ipc.of.world.emit(
|
||||
'hello'
|
||||
)
|
||||
}
|
||||
);
|
||||
|
||||
ipc.of.world.on(
|
||||
'data',
|
||||
function(data){
|
||||
ipc.log('got a message from world : '.debug, data,data.toString());
|
||||
}
|
||||
);
|
||||
}
|
||||
);
|
40
example/TCPSocket/rawBuffer/world.server.js
Normal file
40
example/TCPSocket/rawBuffer/world.server.js
Normal file
|
@ -0,0 +1,40 @@
|
|||
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.rawBuffer=true;
|
||||
ipc.config.encoding='ascii';
|
||||
|
||||
ipc.serveNet(
|
||||
function(){
|
||||
ipc.server.on(
|
||||
'connect',
|
||||
function(socket){
|
||||
ipc.server.emit(
|
||||
socket,
|
||||
'hello'
|
||||
);
|
||||
}
|
||||
);
|
||||
|
||||
ipc.server.on(
|
||||
'data',
|
||||
function(data,socket){
|
||||
ipc.log('got a message'.debug, data,data.toString());
|
||||
ipc.server.emit(
|
||||
socket,
|
||||
'goodbye'
|
||||
);
|
||||
}
|
||||
);
|
||||
}
|
||||
);
|
||||
|
||||
ipc.server.start();
|
35
example/UDPSocket/rawBuffer/hello-client.js
Normal file
35
example/UDPSocket/rawBuffer/hello-client.js
Normal file
|
@ -0,0 +1,35 @@
|
|||
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.rawBuffer=true;
|
||||
ipc.config.encoding='ascii';
|
||||
|
||||
ipc.serveNet(
|
||||
8001, //we set the port here because the world server is already using the default of 8000. So we can not bind to 8000 while world is using it.
|
||||
'udp4',
|
||||
function(){
|
||||
ipc.server.on(
|
||||
'data',
|
||||
function(data){
|
||||
ipc.log('got a message from world '.debug, data, data.toString());
|
||||
}
|
||||
);
|
||||
ipc.server.emit(
|
||||
{
|
||||
address : 'localhost',
|
||||
port : ipc.config.networkPort
|
||||
},
|
||||
'hello'
|
||||
);
|
||||
}
|
||||
);
|
||||
|
||||
ipc.server.start();
|
43
example/UDPSocket/rawBuffer/world.server.js
Normal file
43
example/UDPSocket/rawBuffer/world.server.js
Normal file
|
@ -0,0 +1,43 @@
|
|||
var ipc=require('../../../node-ipc');
|
||||
|
||||
/***************************************\
|
||||
*
|
||||
* UDP Client is really a UDP server
|
||||
*
|
||||
* Dedicated UDP sockets on the same
|
||||
* machine can not be bound to in the
|
||||
* traditional client/server method
|
||||
*
|
||||
* Every UDP socket is it's own UDP server
|
||||
* And so must have a unique port on its
|
||||
* machine, unlike TCP or Unix Sockts
|
||||
* which can share on the same machine.
|
||||
*
|
||||
* Since there is no open client server
|
||||
* relationship, you should start world
|
||||
* first and then hello.
|
||||
*
|
||||
***************************************/
|
||||
|
||||
ipc.config.id = 'world';
|
||||
ipc.config.retry= 1500;
|
||||
ipc.config.rawBuffer=true;
|
||||
ipc.config.encoding='ascii';
|
||||
|
||||
ipc.serveNet(
|
||||
'udp4',
|
||||
function(){
|
||||
ipc.server.on(
|
||||
'data',
|
||||
function(data,socket){
|
||||
ipc.log('got a message'.debug, data,data.toString());
|
||||
ipc.server.emit(
|
||||
socket,
|
||||
'goodbye'
|
||||
);
|
||||
}
|
||||
);
|
||||
}
|
||||
);
|
||||
|
||||
ipc.server.start();
|
|
@ -1,6 +1,7 @@
|
|||
var net = require('net'),
|
||||
eventParser = require('../lib/eventParser.js'),
|
||||
pubsub = require('event-pubsub');
|
||||
pubsub = require('event-pubsub'),
|
||||
Message = require('js-message');
|
||||
|
||||
function init(config,log){
|
||||
var client={
|
||||
|
@ -18,20 +19,18 @@ function init(config,log){
|
|||
|
||||
function emit(type,data){
|
||||
this.log('dispatching event to '.debug, this.id.variable, this.path.variable,' : ', type.data,',', data);
|
||||
if(!data)
|
||||
data=false;
|
||||
|
||||
var message=new Message;
|
||||
message.type=type;
|
||||
message.data=data;
|
||||
|
||||
if(this.config.rawBuffer){
|
||||
data=new Buffer(type,this.encoding);
|
||||
message=new Buffer(type,this.encoding);
|
||||
}else{
|
||||
data=eventParser.format(
|
||||
{
|
||||
type:type,
|
||||
data:data
|
||||
}
|
||||
);
|
||||
message=eventParser.format(message);
|
||||
}
|
||||
|
||||
this.socket.write(data);
|
||||
this.socket.write(message);
|
||||
};
|
||||
|
||||
function connect(){
|
||||
|
@ -153,13 +152,13 @@ function connect(){
|
|||
var events = eventParser.parse(data);
|
||||
var eCount = events.length;
|
||||
for(var i=0; i<eCount; i++){
|
||||
var e=JSON.parse(
|
||||
events[i]
|
||||
);
|
||||
client.log('detected event of type '.debug, e.type.data, e.data);
|
||||
var message=new Message;
|
||||
message.load(events[i]);
|
||||
|
||||
client.log('detected event of type '.debug, message.type.data, message.data);
|
||||
client.trigger(
|
||||
e.type,
|
||||
e.data
|
||||
message.type,
|
||||
message.data
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,11 +1,15 @@
|
|||
function formatData(data){
|
||||
if(!data.data)
|
||||
data.data={};
|
||||
if(data.data['_maxListeners'])
|
||||
delete data.data;
|
||||
var Message = require('js-message');
|
||||
|
||||
data=JSON.stringify(data)+parser.delimiter;
|
||||
return data;
|
||||
function formatData(message){
|
||||
if(!message.data){
|
||||
message.data={};
|
||||
}
|
||||
if(message.data['_maxListeners']){
|
||||
message.data={};
|
||||
}
|
||||
|
||||
message=message.JSON+parser.delimiter;
|
||||
return message;
|
||||
};
|
||||
|
||||
function parseDataEvents(data){
|
||||
|
|
|
@ -2,24 +2,20 @@ var net = require('net'),
|
|||
fs = require('fs'),
|
||||
dgram = require('dgram'),
|
||||
eventParser = require('../lib/eventParser.js'),
|
||||
pubsub = require('event-pubsub');
|
||||
pubsub = require('event-pubsub'),
|
||||
Message = require('js-message');
|
||||
|
||||
function emit(socket, type, data){
|
||||
if(!data)
|
||||
data=false;
|
||||
this.log('dispatching event to socket'.debug, ' : ', type.data, data);
|
||||
|
||||
var event={
|
||||
type:type,
|
||||
data:data
|
||||
}
|
||||
var message=new Message;
|
||||
message.type=type;
|
||||
message.data=data;
|
||||
|
||||
if(this.config.rawBuffer){
|
||||
data=new Buffer(type,this.encoding);
|
||||
message=new Buffer(type,this.encoding);
|
||||
}else{
|
||||
data=eventParser.format(
|
||||
event
|
||||
);
|
||||
message=eventParser.format(message);
|
||||
}
|
||||
|
||||
if(this.udp4 || this.udp6){
|
||||
|
@ -31,41 +27,34 @@ function emit(socket, type, data){
|
|||
}
|
||||
|
||||
this.server.write(
|
||||
data,
|
||||
message,
|
||||
socket
|
||||
);
|
||||
return;
|
||||
};
|
||||
|
||||
socket.write(data);
|
||||
socket.write(message);
|
||||
};
|
||||
|
||||
function broadcast(type,data){
|
||||
this.log('broadcasting event to all known sockets listening to '.debug, this.path.variable,' : ', ((this.port)?this.port:''), type, data);
|
||||
if(!data)
|
||||
data=false;
|
||||
|
||||
var event={
|
||||
type:type,
|
||||
data:data
|
||||
};
|
||||
var message=new Message;
|
||||
message.type=type;
|
||||
message.data=data;
|
||||
|
||||
if(this.config.rawBuffer){
|
||||
data=new Buffer(type,this.encoding);
|
||||
message=new Buffer(type,this.encoding);
|
||||
}else{
|
||||
data=eventParser.format(
|
||||
event
|
||||
);
|
||||
message=eventParser.format(message);
|
||||
}
|
||||
|
||||
|
||||
if(this.udp4 || this.udp6){
|
||||
for(var i=1, count=this.sockets.length; i<count; i++){
|
||||
this.server.write(data,this.sockets[i]);
|
||||
this.server.write(message,this.sockets[i]);
|
||||
}
|
||||
}else{
|
||||
for(var i=0, count=this.sockets.length; i<count; i++){
|
||||
this.sockets[i].write(data);
|
||||
this.sockets[i].write(message);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
@ -202,15 +191,17 @@ function init(path,config,log,port){
|
|||
data=eventParser.parse(data);
|
||||
|
||||
while(data.length>0){
|
||||
var e=JSON.parse(data.shift());
|
||||
server.log('received event of : '.debug,e.type.data,e.data);
|
||||
var message=new Message;
|
||||
message.load(data.shift());
|
||||
|
||||
if(e.data.id)
|
||||
sock.id=e.data.id;
|
||||
server.log('received event of : '.debug,message.type.data,message.data);
|
||||
|
||||
if(message.data.id)
|
||||
sock.id=message.data.id;
|
||||
|
||||
server.trigger(
|
||||
e.type,
|
||||
e.data,
|
||||
message.type,
|
||||
message.data,
|
||||
sock
|
||||
);
|
||||
}
|
||||
|
|
4
node_modules/colors/package.json
generated
vendored
4
node_modules/colors/package.json
generated
vendored
|
@ -16,7 +16,7 @@
|
|||
],
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "http://github.com/Marak/colors.js.git"
|
||||
"url": "git+ssh://git@github.com/Marak/colors.js.git"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=0.1.90"
|
||||
|
@ -27,7 +27,7 @@
|
|||
"shasum": "2423fe6678ac0c5dae8852e5d0e5be08c997abcc",
|
||||
"tarball": "http://registry.npmjs.org/colors/-/colors-0.6.2.tgz"
|
||||
},
|
||||
"_from": "colors@~0.6.2",
|
||||
"_from": "colors@>=0.6.2 <0.7.0",
|
||||
"_npmVersion": "1.2.30",
|
||||
"_npmUser": {
|
||||
"name": "marak",
|
||||
|
|
4
node_modules/event-pubsub/package.json
generated
vendored
4
node_modules/event-pubsub/package.json
generated
vendored
|
@ -11,7 +11,7 @@
|
|||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/RIAEvangelist/event-pubsub.git"
|
||||
"url": "git+https://github.com/RIAEvangelist/event-pubsub.git"
|
||||
},
|
||||
"keywords": [
|
||||
"event",
|
||||
|
@ -33,7 +33,7 @@
|
|||
"shasum": "c81c49b101cdb4892d8fa2631b443184db2de6aa",
|
||||
"tarball": "http://registry.npmjs.org/event-pubsub/-/event-pubsub-1.0.3.tgz"
|
||||
},
|
||||
"_from": "event-pubsub@~1.0.3",
|
||||
"_from": "event-pubsub@>=1.0.3 <1.1.0",
|
||||
"_npmVersion": "1.4.3",
|
||||
"_npmUser": {
|
||||
"name": "riaevangelist",
|
||||
|
|
23
node_modules/node-cmd/package.json
generated
vendored
23
node_modules/node-cmd/package.json
generated
vendored
|
@ -11,7 +11,7 @@
|
|||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/RIAEvangelist/node-cmd.git"
|
||||
"url": "git+https://github.com/RIAEvangelist/node-cmd.git"
|
||||
},
|
||||
"keywords": [
|
||||
"commandline",
|
||||
|
@ -29,10 +29,25 @@
|
|||
"url": "https://github.com/RIAEvangelist/node-cmd/issues"
|
||||
},
|
||||
"homepage": "https://github.com/RIAEvangelist/node-cmd",
|
||||
"readme": "#node-cmd\n-\n*Node.js commandline/terminal interface.* \n\nSimple commandline or terminal interface to allow you to run cli or bash style commands as if you were in the terminal.\n\nRun commands asynchronously, and if needed can get the output as a string.\n\n#Methods\n-\n\n|method | arguments | functionality |\n|-------|-----------|---------------|\n|run | command | runs a command asynchronously|\n|get | command,callback | runs a command asynchronously, when the command is complete all of the stdout will be passed to the callback|\n\n\n#Examples\n-\n\n var cmd=require('node-cmd');\n \n cmd.get(\n 'pwd',\n function(data){\n console.log('the current working dir is : ',data)\n }\n );\n \n cmd.run('touch example.created.file');\n \n cmd.get(\n 'ls',\n function(data){\n console.log('the current dir contains these files :\\n\\n',data)\n }\n );\n\n",
|
||||
"readmeFilename": "README.md",
|
||||
"gitHead": "a003426996e8594af31a6486cfb7d28d0a547bc9",
|
||||
"_id": "node-cmd@1.0.2",
|
||||
"_shasum": "89cdb50181476cdd127763d5c8514499fe3c038d",
|
||||
"_from": "node-cmd@~1.0.1"
|
||||
"_from": "node-cmd@>=1.0.1 <1.1.0",
|
||||
"_npmVersion": "1.4.23",
|
||||
"_npmUser": {
|
||||
"name": "riaevangelist",
|
||||
"email": "brandon@diginow.it"
|
||||
},
|
||||
"maintainers": [
|
||||
{
|
||||
"name": "riaevangelist",
|
||||
"email": "brandon@1942design.com"
|
||||
}
|
||||
],
|
||||
"dist": {
|
||||
"shasum": "89cdb50181476cdd127763d5c8514499fe3c038d",
|
||||
"tarball": "http://registry.npmjs.org/node-cmd/-/node-cmd-1.0.2.tgz"
|
||||
},
|
||||
"_resolved": "https://registry.npmjs.org/node-cmd/-/node-cmd-1.0.2.tgz",
|
||||
"readme": "ERROR: No README data found!"
|
||||
}
|
||||
|
|
|
@ -7,15 +7,12 @@
|
|||
"example": "example"
|
||||
},
|
||||
"dependencies": {
|
||||
"event-pubsub": "~1.0.3",
|
||||
"colors": "~0.6.2",
|
||||
"event-pubsub": "~1.0.3",
|
||||
"js-message": "*",
|
||||
"node-cmd": "~1.0.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"colors": "*",
|
||||
"event-pubsub": "*",
|
||||
"node-cmd": "*"
|
||||
},
|
||||
"devDependencies": {},
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
|
|
Loading…
Reference in a new issue