-
Notifications
You must be signed in to change notification settings - Fork 0
/
zyre-join.js
53 lines (45 loc) · 1.12 KB
/
zyre-join.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
module.exports = function(RED) {
function ZyreJoin(config) {
RED.nodes.createNode(this, config)
this.topic = config.topic
this.zyre = RED.nodes.getNode(config.zyre).zyre
let peer = this.zyre._name
this.status({
fill: 'blue',
shape: 'dot',
text: peer
})
let onJoin = (identity, name, group) => {
this.debug(`${name} has joined ${group}`)
let msg = {
topic: group,
payload: {
identity,
name,
group
}
}
this.send(msg)
}
let onInput = (msg, send, done) => {
let topic = this.topic || msg.topic || msg.payload
this.debug(`${peer} is joining ${topic}`)
this.zyre.join(topic)
if (done) {
done()
}
}
let onClose = (removed, done) => {
this.debug(`Removing listener for ${peer}`)
this.zyre.off('join', onJoin)
if (done) {
done()
}
}
this.debug(`Registering listener for ${peer}`)
this.zyre.on('join', onJoin)
this.on('input', onInput)
this.on('close', onClose)
}
RED.nodes.registerType('zyre-join', ZyreJoin)
}