-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzyre-connect.js
More file actions
41 lines (35 loc) · 899 Bytes
/
zyre-connect.js
File metadata and controls
41 lines (35 loc) · 899 Bytes
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
module.exports = function(RED) {
function ZyreConnect(config) {
RED.nodes.createNode(this, config)
this.zyre = RED.nodes.getNode(config.zyre).zyre
let peer = this.zyre._name
this.status({
fill: 'blue',
shape: 'dot',
text: peer
})
let onConnect = (identity, name, headers) => {
this.log(`${peer} is now connected to ${name}`)
let msg = {
topic: identity,
payload: {
identity,
name,
headers
}
}
this.send(msg)
}
let onClose = (removed, done) => {
this.debug(`Removing listener for ${peer}`)
this.zyre.off('connect', onConnect)
if (done) {
done()
}
}
this.debug(`Registering listener for ${peer}`)
this.zyre.on('connect', onConnect)
this.on('close', onClose)
}
RED.nodes.registerType('zyre-connect', ZyreConnect)
}