-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsocket_client.js
More file actions
43 lines (43 loc) · 1.31 KB
/
Copy pathsocket_client.js
File metadata and controls
43 lines (43 loc) · 1.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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");