forked from kubovy/elko-ws-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathelko-ws-client.js
42 lines (30 loc) · 999 Bytes
/
elko-ws-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
"use strict";
const child_process = require('child_process');
const WebSocket = require('ws');
const fs = require('fs');
const DEBUG = false;
console.log("Starting...");
let config = JSON.parse(fs.readFileSync('config.json', 'utf8'));
if (DEBUG) console.log(config);
let subprocess = child_process.fork(`${__dirname}/elko-ws-process.js`);
let webservices = [];
for (let i in config['elko']) {
let host = config['elko'][i];
let ws = new WebSocket(config['ws'].replace("{{HOST}}", host));
ws.on('open', function open() {
console.log('[WS] Client connected');
});
ws.on('close', function close() {
console.log('[WS] Client disconnected');
});
ws.on('message', function incoming(data) {
console.log("[WS] Incoming (" + host + "):", data);
let pattern = RegExp(config['regex']);
let matches = pattern.exec(data);
if (matches && matches.length > 1) {
let deviceId = matches[1];
subprocess.send({"deviceId": deviceId, "source": host})
}
});
webservices.push(ws);
}