Skip to content

Commit

Permalink
Added some socket examples
Browse files Browse the repository at this point in the history
  • Loading branch information
Sean Robinson authored and Sean Robinson committed Aug 7, 2014
1 parent 1bfe6ae commit 65cf079
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
4 changes: 2 additions & 2 deletions CodeSamples/ch08/socket_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ function writeData(socket, data){
writeData(socket, data);
});
})(socket, data);
}
}
}
var Dwarves = getConnection("Dwarves");
var Elves = getConnection("Elves");
var Hobbits = getConnection("Hobbits");
writeData(Dwarves, "More Axes");
writeData(Elves, "More Arrows");
writeData(Hobbits, "More Pipe Weed");
writeData(Hobbits, "More Pipe Weed");
43 changes: 43 additions & 0 deletions Samples/Sockets/socket_client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
var net = require('net');
function getConnection(connName){
var client = net.connect({port: 8107, 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 + " 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 Dwarves = getConnection("Dwarves");
var Elves = getConnection("Elves");
var Hobbits = getConnection("Hobbits");
writeData(Dwarves, "More Axes");
writeData(Elves, "More Arrows");
writeData(Hobbits, "More Pipe Weed");

0 comments on commit 65cf079

Please sign in to comment.