-
Notifications
You must be signed in to change notification settings - Fork 0
/
EchoClientServer.js
153 lines (134 loc) · 3.51 KB
/
EchoClientServer.js
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
* Echo Server & Client
*
*/
var s = require('net');
//http://stackoverflow.com/questions/221294/how-do-you-get-a-timestamp-in-javascript
function getTimeStamp() {
var now = new Date();
return ((now.getMonth() + 1) + '/' +
(now.getDate()) + '/' +
now.getFullYear() + " " +
now.getHours() + ':' +
((now.getMinutes() < 10)
? ("0" + now.getMinutes())
: (now.getMinutes())) + ':' +
((now.getSeconds() < 10)
? ("0" + now.getSeconds())
: (now.getSeconds())));
}
var util = {
//Function parses command line parameters (argv) and checks to see if exists in array, if so returns bool, number, or string
getARGVs: function( argv, array, type ){
for( var i=0, l = argv.length; i < l; i+=1) {
if( array.indexOf( argv[i]) != -1 ){
if( type == "bool"){
return true;
}else {
if( typeof argv[i+1] !== "undefined") {
if( type == "number"){
return parseInt( argv[i+1] );
}else {
return argv[i+1];
}
}
}
}
}
if( type == "bool"){
return false;
}else{
return 0;
}
},
//Function checks the port for validity and then assigns to obj property and returns true when valid, otherwise false
setPort: function( obj, port){
if( typeof port !== "undefined" && typeof port === 'number' && port > 0 )
{
obj.port = port;
return true;
}
return false;
}
}
var serverAPI = (function(s){
var quietLog = false,
port = 3000,
server,
api;
api = {
start: function( options ) {
//Initializes the port and quietLog flags
var obj = {};
if( util.setPort( obj, options["port"]) ){
port = obj.port;
}
delete obj;
quietLog = options["quiet"];
//Creates the Server
server = s.createServer( function( socket ) {
//Inside the server handling function
socket.on('data', function(data){
//Inside the server listener function when data detected
var log = "";
if( !quietLog ) {
log += "\n/********** RECEIVED **************/";
log += "\n/ Message: " + data;
log += "\n/ Source IP: " + socket.remoteAddress + ':' +socket.remotePort;
log += "\n/ TimeStamp:" + getTimeStamp();
log += "\n/***********************************/\n";
console.log( log );
}
//Echo the data detected back to the client
socket.write( data );
});
}).listen( port );
return s;
},
close: function() {
server.close();
}
};
return api;
}(s));
var clientAPI = (function(s){
var client = new s.Socket(),
port = 3000,
timer = { start: 0, end: 0, show: false },
api;
client.setEncoding('utf8');
api = {
send: function( msg, options){
//Initializes the port and timer flags
var obj = {};
if( util.setPort( obj, options.port ) ) {
port = obj.port;
}
if( options.timer ){
timer.show = true;
}
delete obj;
if( timer.show ){
timer.start = new Date().getTime();
}
client.connect( port, 'localhost', function(){
//Inside the succesful connection function
client.write( msg );
});
},
listen: function( fn ){
//Inside listen we pass a function through parameter to capture custom data handling
client.on( 'data', function(data){
if( timer.show ){
console.log( "Response time: "+ (new Date().getTime() - timer.start ) +"ms" );
}
fn( data );
client.destroy();
});
}
};
return api;
}(s));
module.exports.util = util;
module.exports.server = serverAPI;
module.exports.client = clientAPI;