-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.js
204 lines (171 loc) · 5.05 KB
/
client.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
"use strict"
const net = require('net');
var uuid = require('uuid/v4');
var EventEmitter = require('events').EventEmitter;
const util = require('util');
var ChallengeClient = function() {
var that = this;
this.client = null;
this.heartbeatSeen = 0;
var _server = null;
var _port = null;
var _timeout= null;
var _loginObj = null;
var _debug = false;
this.connect = function(args) {
_server = args.server;
_port = args.port;
_timeout = args.timeout;
_loginObj = args.loginObj;
_debug = args.debug;
_connect();
}
function _connect() {
that.client = new net.Socket();
that.client.setEncoding('utf8');
_initHandlers();
try {
that.client.connect(_port, _server, function() {
console.log('Connected');
that.client.setTimeout(_timeout + 1);
_startHeartbeatTimer(_timeout);
that.emit('connected', '');
});
}
catch (e) {
console.log(e);
}
}
function _checkConnected() {
if (that.client != null) {
var ra = that.client.remoteAddress;
if (typeof ra != 'undefined')
return true;
}
return false;
}
function _initHandlers() {
that.client.on('close', () => {
console.log('Client connection closed');
_disConnect();
_connect();
});
that.client.on('error', (e) => {
console.log(e);
that.emit('socketError', e)
});
that.client.on('timeout', () => {
// _pdebug('socket timeout');
_checkHeartbeat();
that.emit('timedout', '');
});
that.client.on('data', (incoming) => {
// _pdebug('Received: ' + incoming);
// _pdebug('>>> end');
var lines = incoming.split('\n');
if (lines.length > 1){
for (var i in lines) {
lines[i] = lines[i].trim();
if (lines[i].length > 0)
_handleIncoming(lines[i])
}
}
else
_handleIncoming(incoming);
});
}
function _handleIncoming(incoming) {
try {
var inData = JSON.parse(incoming);
}
catch (err) {
// _pdebug(err);
that.emit('dataParseError', err);
return;
}
if ('type' in inData) {
var type = inData.type;
if (type == 'heartbeat') {
that.heartbeatSeen = Date.now();
}
else if (type == 'msg') {
if ('reply' in inData.msg) {
var cid = inData.msg.reply;
var callback = _cidChecker.checkCID(cid);
// _pdebug('cid ok ' + valid);
if (callback) {
callback(inData);
}
}
}
that.emit(type, inData);
}
}
function _disConnect() {
_stopHeartbeatTimer();
if (that.client) {
that.client.end();
that.client.destroy();
that.client = null;
}
}
// could make this take an array of send objects and loop
// through them to build a string of '\n'-delimited messages
this.send = (sendObj, callback) => {
if (_checkConnected() != true)
return; // could queue up sends?
if (callback) {
var cid = _cidChecker.putCID(callback);
sendObj.id = cid;
}
_pdebug('sending: ');
_pdebug(sendObj);
var sendData = JSON.stringify(sendObj);
that.client.write(sendData);
}
function _checkHeartbeat() {
var now = Date.now();
var thbs = that.heartbeatSeen;
if ( (now - that.heartbeatSeen) > 2000) {
// _pdebu.log('heartbeat timed out');
_disConnect();
}
}
var intervalID = null;
function _startHeartbeatTimer(timeout) {
intervalID = setInterval(function(){ _checkHeartbeat(); }, timeout);
}
function _stopHeartbeatTimer() {
if (intervalID != null) {
clearInterval(intervalID);
intervalID = null;
}
that.heartbeatSeen = 0;
}
function _pdebug(msg) {
if (_debug) {
console.log(msg);
}
}
var _cidChecker = new CidChecker();
}
var CidChecker = function() {
var cids = {};
this.putCID = (callback) => {
var cid = uuid();
cids[cid] = callback;
return(cid);
}
this.checkCID = (cid) => {
if (cid in cids) {
var callback = cids[cid];
delete cids[cid];
return callback;
}
else
return null;
}
}
// extend the EventEmitter class using our ChallengeClient class
util.inherits(ChallengeClient, EventEmitter);
module.exports.ChallengeClient = ChallengeClient;