-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
62 lines (60 loc) · 2.31 KB
/
index.ts
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
import { RabbitMq } from './rabbitmq';
import { Source, Destination } from './rabbitmqcreds'
import { Exchanges, ExchangeType } from './exchanges';
var sourceRabbit = new RabbitMq(Source);
var destinationRabbit = new RabbitMq(Destination);
interface MessageInterval {
time: number,
msg: Buffer
}
// const msgInterval: { [key: number]: any; } = {};
const msgInterval : MessageInterval[] = [];
sourceRabbit.init(() => {
console.log('Rabbitmq connected')
destinationRabbit.init(() => {
Exchanges.forEach(exchange => {
if(exchange.type == ExchangeType.Both) {
sourceRabbit.connect(exchange.exchangeName, (msg, routeKey) => {
var msg1 = msg.toString();
if(msgInterval.map(el => el.msg).indexOf(msg1) < 0)
{
msgInterval.push({
time: Date.now(),
msg: msg1
});
destinationRabbit.publish(exchange.exchangeName, routeKey, msg);
}
})
destinationRabbit.connect(exchange.exchangeName, (msg, routeKey) => {
var msg1 = msg.toString();
if(msgInterval.map(el => el.msg).indexOf(msg1) < 0)
{
msgInterval.push({
time: Date.now(),
msg: msg1
});
sourceRabbit.publish(exchange.exchangeName, routeKey, msg);
}
})
}
else if(exchange.type == ExchangeType.Source) {
sourceRabbit.connect(exchange.exchangeName, (msg, routeKey) => {
destinationRabbit.publish(exchange.exchangeName, routeKey, msg);
})
}
else if(exchange.type == ExchangeType.Destination) {
destinationRabbit.connect(exchange.exchangeName, (msg, routeKey) => {
sourceRabbit.publish(exchange.exchangeName, routeKey, msg);
})
}
})
});
});
setInterval(() => {
const dt = Date.now() - (3 * 1000);
msgInterval.forEach((msg, index) => {
if(msg.time < dt) {
msgInterval.splice(index, 1);
}
});
}, 1000)