-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
123 lines (99 loc) · 2.89 KB
/
index.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
var util = require('util'),
events = require('events'),
amqp = require('amqp');
function RabbitmqEvent(connection, exchangeName, queueList) {
"use strict";
events.EventEmitter.call(this);
var self = this;
self.exchangeName = exchangeName;
self._connectedCount=0;
if (!queueList || queueList.length === 0) {
throw new Error("No queues specified to RabbitmqEvent");
}
if (!connection.host) {
throw new Error("No hostname specified to RabbitmqEvent");
}
this.queueList = queueList;
// Publish
this.pubRabbit = amqp.createConnection(connection);
this.pubRabbit.on('error', function(err){
console.log(err);
});
this.pubRabbit.on('ready', function(conn){
self._connectedCount++;
self._exchange();
if (self._connectedCount == 2) {
self.emit('ready');
}
});
this.pubRabbit.on('end', function() {self._connectedCount--; });
// Subscribe
this.subRabbit = amqp.createConnection(connection);
this.subRabbit.on('error', function(err){
console.log(err);
});
this.subRabbit.on('ready', function(conn) {
self._connectedCount++;
self._subscribe();
if (self._connectedCount == 2) {
self.emit('ready');
}
});
this.subRabbit.on('message', this._onMessage.bind(this));
this.subRabbit.on('end', function() {self._connectedCount--; });
}
util.inherits(RabbitmqEvent, events.EventEmitter);
RabbitmqEvent.prototype._exchange = function() {
this.exchange = this.pubRabbit.exchange(this.exchangeName, {type: 'topic'});
};
RabbitmqEvent.prototype._subscribe = function() {
var self = this;
this.queueList.forEach(function(queueName) {
self.subRabbit.queue(queueName, function(queue){
queue.bind(self.exchangeName, queueName);
queue.subscribe(function(message, headers, payload){
self.subRabbit.emit('message', message, headers, payload);
});
});
});
};
RabbitmqEvent.prototype._onMessage = function(message, headers, payload) {
var eventName = null, channelName = null;
channelName = payload.routingKey;
if (headers.event !== undefined) {
eventName = headers.event;
} else {
eventName = message;
}
try {
eventName = channelName + ':' + eventName;
} catch(e) {
}
if (eventName) {
this.emit(eventName, message);
}
};
RabbitmqEvent.prototype.pub = function(eventName, payload, stringPayload) {
var task = eventName.split(':');
if (task.length!=2) {
console.log("eventName '%s' is incorrect", eventName);
return false;
}
var options = {
contentType: 'application/octet-stream',
headers: {
event: task[1],
dataType: 'raw'
}
};
if (stringPayload) {
options.contentType = 'text/json';
options.headers.dataType = 'string';
}
this.exchange.publish(task[0], payload, options);
};
RabbitmqEvent.prototype.quit = function() {
this.subRabbit.disconnect();
this.pubRabbit.disconnect();
};
module.exports = RabbitmqEvent;