forked from Treyone/ha-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathha-connect.js
58 lines (54 loc) · 1.34 KB
/
ha-connect.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
/**
* Created by ycorneille on 21/03/14.
*/
var net = require('net');
/**
* Connects to HA Socket
* @param socket
* @constructor
*/
var haConnector = function(socket){
var _socket = socket;
/**
* connects to HAProxy through the specified socket
* @param callback
*/
var connectToHA = function(callback){
try{
var client = net.connect(_socket, function() {
callback && callback(null,client);
});
}
catch(ex){
callback && callback(ex);
}
};
/**
* sends a message to HA, gathers the response and passes it to the callback
* @param message
* @param callback
*/
var _talkToHa = function(message,callback){
connectToHA(function(err,ha){
if(err){return (callback && callback(err));}
var output = "";
ha.on('data', function(data) {
output += data;
});
ha.on('end', function() {
callback && callback(null,output);
ha.end();
});
if(message.indexOf('\n'==-1)){message+='\n';}
//console.log('sending '+message)
ha.write(message);
});
}
/**
* Exposed methods
*/
return {
send : _talkToHa
}
}
module.exports = haConnector;