-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
51 lines (44 loc) · 1.42 KB
/
index.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
/**
*
* getEncivInfo uses socket.io to send a request to the socket based api server, and then return the response in a call back.
*
*
* the socket connection is persistent, and it is authenticated.
*
*/
const clientIo = require("socket.io-client");
// ensure ENV keys
if (!process.env.ENCIV_API_KEY) {
console.error("ENCIV_API_KEY needed. On bash use: export ENCIV_API_KEY=\"your-key-here\" or add it to your .bashrc file")
process.exit();
}
// ensure ENV keys
if (!process.env.ENCIV_API_URL) {
console.error("ENCIV_API_URL needed. On bash use: export ENCIV_API_URL=\"your-key-here\" or add it to your .bashrc file")
process.exit();
}
const ioClient = clientIo.connect(process.env.ENCIV_API_URL);
var authenticated=false;
var queued=[];
ioClient.on('connect',()=>{
console.info("client connected", ioClient.id);
ioClient.emit('authenticate', process.env.ENCIV_API_KEY);
ioClient.on("authenticated",()=>{
authenticated=true;
while(queued.length) queued.shift()();
})
});
function disconnect(...args){
ioClient.emit('force-disconnect','exit');
return ioClient.close();
}
function getEncivInfo(...args) {
if(args[0]==='disconnect') {
if(!authenticated) queued.push(disconnect);
else disconnect(...args);
} else if(!authenticated)
queued.push(()=>ioClient.emit(...args))
else
ioClient.emit(...args)
}
module.exports=getEncivInfo;