-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathamqp-input.js
64 lines (50 loc) · 2.03 KB
/
amqp-input.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
54
55
56
57
58
59
60
61
62
63
64
function showStatus(node, msgCounter) {
if (msgCounter >= 1) {
node.status({fill: "blue", shape: "dot", text: `handling tasks ${msgCounter}`});
} else {
node.status({fill: "blue", shape: "ring", text: `subcribed ${msgCounter}`});
}
}
module.exports = function(RED) {
function AMQPInput(config) {
RED.nodes.createNode(this,config);
var node = this;
const amqpServer = RED.nodes.getNode(config.amqpServer);
async function initNode() {
var connection = amqpServer.connection;
if (!connection) {
return;
}
const routingKey = (config.exchangeType == 'topic') ? RED.util.evaluateNodeProperty(config.routingKey, config.routingKeyFieldType, node) : '';
const channel = await connection.createChannel();
await channel.assertExchange(config.exchange, config.exchangeType);
const queue = await channel.assertQueue(config.queue);
await channel.bindQueue(queue.queue, config.exchange, routingKey);
await channel.consume(queue.queue, async (message) => {
const msg = {};
try {
msg.payload = JSON.parse(message.content.toString());
} catch (e) {
msg.payload = message.content.toString();
}
node.send(msg);
channel.ack(message);
});
const onCloseHandlerId = connection.onClose(async () => {
console.log('onClose connection');
await initNode();
});
node.on("close", async () => {
console.log('closeChannel');
try {
await channel.close();
connection.removeOnCloseHandler(onCloseHandlerId);
} catch {
console.warn('Channel closed');
}
});
}
initNode();
}
RED.nodes.registerType("amqp-input", AMQPInput);
}