forked from pardsrl/pardsrl-petrolink-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Client.js
52 lines (38 loc) · 1.24 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
'use strict'
const net = require('net');
const debug = require('debug')('pardsrl-petrolink:client')
class Client {
constructor(config){
this._config = config
this._client = new net.Socket()
this._intervalConnect = false
this._client.on('connect', () => {
this.clearIntervalConnect()
debug('TCP','connected to server')
this._client.write('CLIENT connected');
})
this._client.on('error', (err) => {
debug('[TCP ERROR]', err.code )
this.launchIntervalConnect()
})
this._client.on('close', this.launchIntervalConnect)
this._client.on('end', this.launchIntervalConnect)
}
connect() {
debug('Client Trying to connect')
this._client.connect({
host: this._config.host,
port: this._config.port
})
}
launchIntervalConnect() {
if(false != this._intervalConnect) return
this._intervalConnect = setInterval(() => this.connect() , 5000)
}
clearIntervalConnect() {
if(false == this._intervalConnect) return
clearInterval(this._intervalConnect)
this._intervalConnect = false
}
}
module.exports = Client;