-
Notifications
You must be signed in to change notification settings - Fork 0
/
syfterListener.js
48 lines (41 loc) · 1.67 KB
/
syfterListener.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
const SocketClient = require('socket.io-client');
module.exports = function(RED) {
function SyfterListener(config) {
RED.nodes.createNode(this, config);
this.name = config.name;
this.documentType = config.documentType;
var node = this;
if (!this.documentType) {
node.error('Invalid Document Type');
return;
}
this.syfterConfig = RED.nodes.getNode(config.syfterConfig);
if (!this.syfterConfig || !this.syfterConfig.config || !this.syfterConfig.config.socketHost || !this.syfterConfig.config.apiKey) {
node.error('Invalid Syfter config');
return;
}
const options = { path: '/ws', query: { 'Api-Key': this.syfterConfig.config.apiKey } }
this.socket = new SocketClient(this.syfterConfig.config.socketHost, options);
this.socket.on('connect', function() {
node.status({ fill: 'green', shape: 'dot', text: 'connected' });
});
this.socket.on('disconnect', function () {
node.status({ fill: 'red', shape: 'ring', text: 'disconnected' });
});
this.socket.on('connect_error', function(err) {
if (err) {
node.status({ fill: 'red', shape: 'ring', text: 'error' });
node.send({ payload: err });
}
});
this.socket.on(node.documentType, function (data) {
node.send({ payload: data });
})
node.on('close', function (done) {
node.socket.disconnect();
node.status({});
done();
});
}
RED.nodes.registerType('filament-syfter-listener', SyfterListener);
};