样例是单机利用nodejs启动server.js创建4个server,收听发来的消息。再启动client.js让两个或三个client互发消息。
今后可以将节点做成收发服务一体,收服务将数据发送给可视服务器visualserver。例如Client.js中A给B节点发消息,B给A节点发消息,A和B节点收到消息后分别给VisualServer发消息。样例中server.js将各个server服务程序发来的消息集中在一起来启动进行消息收听。clientA.js中的多个client发消息给其他节点。也可以将节点程序分散在不同serverX.js和clientX.js中。
server.js内容:
var net = require('net');
//for visual server
var visualserverHOST = '127.0.0.1';
var visualserverPORT = 8848;
var vserver;
var server = net.createServer(function(client){// this client is a socket object.
console.log('visualserver created, Client connection:');
console.log(' local = %s:%s',client.localAddress,client.localPort);
console.log(' remote = %s:%s',client.remoteAddress,client.remotePort);
client.setTimeout(500);
client.setEncoding('utf8');
client.on('data',function(data){
console.log('visualserver Created and Received data from client port %d: %s: %s',client.remotePort,data.toString(),new Date().getTime());
console.log(' Bytes received: '+client.bytesRead);
writeData(client,'visualserver Sending back to client: '+data.toString());
console.log(' Bytes sent:' + client.bytesWritten);
});
client.on('end',function(){
console.log('Client disconnected');
server.getConnections(function(err,count){
console.log('Remaining Connections: ' + count);
});
});
client.on('error',function(err){
console.log('Socket Error: ',JSON.stringify(err));
});
client.on('timeout',function(){
console.log('Socket Timed out');
});
});
server.listen(visualserverPORT, function(){
//console.log('Server listening: '+JSON.stringify(server.address));
console.log('visual Server listening: '+ visualserverHOST + ':' + visualserverPORT);
server.on('close',function(){
console.log('visual Server Terminated');
});
server.on('error',function(err){
console.log('visual Server Error : '+JSON.stringify(err));
});
});
function writeData(socket,data){
var success = !socket.write(data);
if(!success){
(function(socket,data){
socket.once('drain',function(){
writeData(socket,data);
});
})(socket,data);
}
}
function getConnection(connName, serverPORT){
var client = net.connect({port:serverPORT,host:'localhost'},function(){
console.log(connName + ' has been Connected:');
console.log(' local=%s:%s',this.localAddress,this.localPort);
console.log(' remote = %s:%s',this.remoteAddress,this.remotePort);
this.setTimeout(500);
this.setEncoding('utf8');
this.on('data',function(data){
console.log(connName + " connected and received data: " + data.toString());
});
this.on('end',function(){
console.log(connName + ' disconnected');
});
this.on('error',function(err){
console.log('Socket Error: ',JSON.stringify(err));
});
this.on('timeout',function(){
console.log(connName + 'Socket Timed Out');
});
this.on('close',function(){
console.log(connName + ' Socket Closed');
});
});
return client;
}
//for ClientA's server
var ClientAsHOST = '127.0.0.1';
var ClientAsPORT = 1001;
var clientAserver = net.createServer(function(client){// this client is a socket object.
console.log('clientAserver created, Client connection:');
console.log(' local = %s:%s',client.localAddress,client.localPort);
console.log(' remote = %s:%s',client.remoteAddress,client.remotePort);
client.setTimeout(500);
client.setEncoding('utf8');
client.on('data',function(data){
console.log('ClientAserver Created and Received data from clients port %d:%s:%s',client.remotePort,data.toString(),new Date().getTime());
console.log(' Bytes received: '+client.bytesRead);
writeData(client,'Sending: '+data.toString());
console.log(' Bytes sent:' + client.bytesWritten);
//write to visual server
vserver = getConnection("visualserver",visualserverPORT)
writeData(vserver, data.toString()+new Date().getTime());
});
client.on('end',function(){
console.log('Client disconnected');
clientAserver.getConnections(function(err,count){
console.log('Remaining Connections: ' + count);
});
});
client.on('error',function(err){
console.log('Socket Error: ',JSON.stringify(err));
});
client.on('timeout',function(){
console.log('Socket Timed out');
});
});
clientAserver.listen(ClientAsPORT, function(){
//console.log('Server listening: '+JSON.stringify(server.address));
console.log('ClientAServer listening: '+ ClientAsHOST + ':' + ClientAsPORT);
clientAserver.on('close',function(){
console.log('ClientAServer Terminated');
});
clientAserver.on('error',function(err){
console.log('ClientAServer Error : '+JSON.stringify(err));
});
});
//for ClientB's server
var ClientBsHOST = '127.0.0.1';
var ClientBsPORT = 1002;
//var vserver = getConnection("visualserver",visualserverPORT)
var clientBserver = net.createServer(function(client){// this client is a socket object.
console.log('clientBserver created, Client connection:');
console.log(' local = %s:%s',client.localAddress,client.localPort);
console.log(' remote = %s:%s',client.remoteAddress,client.remotePort);
client.setTimeout(500);
client.setEncoding('utf8');
client.on('data',function(data){
console.log('ClientBserver Created and Received data from clients port %d:%s:%s',client.remotePort,data.toString(),new Date().getTime());
console.log(' Bytes received: '+client.bytesRead);
writeData(client,'Sending: '+data.toString());
console.log(' Bytes sent:' + client.bytesWritten);
//write to visual server
vserver = getConnection("visualserver",visualserverPORT)
writeData(vserver, data.toString()+new Date().getTime());
});
client.on('end',function(){
console.log('Client disconnected');
clientBserver.getConnections(function(err,count){
console.log('Remaining Connections: ' + count);
});
});
client.on('error',function(err){
console.log('Socket Error: ',JSON.stringify(err));
});
client.on('timeout',function(){
console.log('Socket Timed out');
});
});
clientBserver.listen(ClientBsPORT, function(){
//console.log('Server listening: '+JSON.stringify(server.address));
console.log('ClientBServer listening: '+ ClientBsHOST + ':' + ClientBsPORT);
clientBserver.on('close',function(){
console.log('ClientBServer Terminated');
});
clientBserver.on('error',function(err){
console.log('ClientBServer Error : '+JSON.stringify(err));
});
});
//for ClientC's server
var ClientCsHOST = '127.0.0.1';
var ClientCsPORT = 1003;
var clientCserver = net.createServer(function(client){// this client is a socket object.
console.log('clientCserver created, Client connection:');
console.log(' local = %s:%s',client.localAddress,client.localPort);
console.log(' remote = %s:%s',client.remoteAddress,client.remotePort);
client.setTimeout(500);
client.setEncoding('utf8');
client.on('data',function(data){
console.log('clientCserver Created and Received data from clients port %d:%s:%s',client.remotePort,data.toString(),new Date().getTime());
console.log(' Bytes received: '+client.bytesRead);
writeData(client,'Sending: '+data.toString());
console.log(' Bytes sent:' + client.bytesWritten);
//write to visual server
vserver = getConnection("visualserver",visualserverPORT)
writeData(vserver, data.toString()+new Date().getTime());
});
client.on('end',function(){
console.log('Client disconnected');
clientCserver.getConnections(function(err,count){
console.log('Remaining Connections: ' + count);
});
});
client.on('error',function(err){
console.log('Socket Error: ',JSON.stringify(err));
});
client.on('timeout',function(){
console.log('Socket Timed out');
});
});
clientCserver.listen(ClientCsPORT, function(){
//console.log('Server listening: '+JSON.stringify(server.address));
console.log('ClientCServer listening: '+ ClientCsHOST + ':' + ClientCsPORT);
clientCserver.on('close',function(){
console.log('ClientCServer Terminated');
});
clientBserver.on('error',function(err){
console.log('ClientCServer Error : '+JSON.stringify(err));
});
});
ClientA.js内容:
var net = require('net');
//for Client functions
var ServerAsPORT = 1001;
var ServerBsPORT = 1002;
var ServerCsPORT = 1003;
function getConnection(connName, clientserverPORT){
var client = net.connect({port:clientserverPORT,host:'localhost'},function(){
console.log(connName + ' Connected:');
console.log(' local=%s:%s',this.localAddress,this.localPort);
console.log(' remote = %s:%s',this.remoteAddress,this.remotePort);
this.setTimeout(500);
this.setEncoding('utf8');
this.on('data',function(data){
console.log(connName + " receiving from Server: " + data.toString());
this.end();
});
this.on('end',function(){
console.log(connName + 'Client disconnected');
});
this.on('error',function(err){
console.log('Socket Error: ',JSON.stringify(err));
});
this.on('timeout',function(){
console.log('Socket Timed Out');
});
this.on('close',function(){
console.log('Socket Closed');
});
});
return client;
}
function writeData(socket,data){
var success = !socket.write(data);
if(!success){
(function(socket,data){
socket.once('drain',function(){
writeData(socket,data);
});
})(socket,data);
}
}
var ClientA = getConnection("ClientA", ServerBsPORT);
var ClientB = getConnection("ClientB", ServerAsPORT);
// var ClientC = getConnection("ClientC");
writeData(ClientA,"More Axes " + new Date().getTime());
writeData(ClientB,"More Bus " + new Date().getTime());
// writeData(ClientC,"More Cow " + new Date().getTime());
server.js的命令行输出:
clientA.js输出: