1 | const events = require('events');
|
2 |
|
3 | class Transmitter extends events {
|
4 | constructor(channel, confirmChannel) {
|
5 | super();
|
6 | this._channel = channel;
|
7 | this.confirmChannel = confirmChannel;
|
8 | }
|
9 |
|
10 | set channel(value) {
|
11 | this._channel = value;
|
12 | }
|
13 |
|
14 | waitForConfirms() {
|
15 | return this._channel.waitForConfirms();
|
16 | }
|
17 |
|
18 | send(queue, msg, options={}) {
|
19 | let that = this;
|
20 | if(typeof msg != 'string') msg = JSON.stringify(msg);
|
21 | if(!this.confirmChannel) {
|
22 | this._channel.sendToQueue(queue, Buffer.from(msg), options);
|
23 | } else {
|
24 | this._channel.sendToQueue(queue, Buffer.from(msg), options, (err, ok) => {
|
25 | if(err) that.emit('error', err);
|
26 | });
|
27 | }
|
28 | }
|
29 |
|
30 | publish(exchange, routingKey, msg, options={}) {
|
31 | let that = this;
|
32 | if(typeof msg != 'string') msg = JSON.stringify(msg);
|
33 | if(!this.confirmChannel) {
|
34 | this._channel.publish(exchange, routingKey, Buffer.from(msg), options);
|
35 | } else {
|
36 | this._channel.publish(exchange, routingKey, Buffer.from(msg), options, (err, ok) => {
|
37 | if(err) that.emit('error', err);
|
38 | });
|
39 | }
|
40 | }
|
41 | }
|
42 |
|
43 | module.exports = Transmitter;
|