|
|
@ -1,31 +1,35 @@
|
|
|
|
'use strict';
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
|
|
var nconf = require('nconf');
|
|
|
|
const nconf = require('nconf');
|
|
|
|
var util = require('util');
|
|
|
|
const util = require('util');
|
|
|
|
var winston = require('winston');
|
|
|
|
const winston = require('winston');
|
|
|
|
var EventEmitter = require('events').EventEmitter;
|
|
|
|
const EventEmitter = require('events').EventEmitter;
|
|
|
|
const connection = require('./connection');
|
|
|
|
const connection = require('./connection');
|
|
|
|
|
|
|
|
|
|
|
|
var channelName;
|
|
|
|
let channelName;
|
|
|
|
var PubSub = function () {
|
|
|
|
const PubSub = function () {
|
|
|
|
var self = this;
|
|
|
|
const self = this;
|
|
|
|
var subClient = connection.connect();
|
|
|
|
|
|
|
|
this.pubClient = connection.connect();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
channelName = 'db:' + nconf.get('redis:database') + ':pubsub_channel';
|
|
|
|
channelName = 'db:' + nconf.get('redis:database') + ':pubsub_channel';
|
|
|
|
subClient.subscribe(channelName);
|
|
|
|
|
|
|
|
|
|
|
|
connection.connect().then(function (client) {
|
|
|
|
subClient.on('message', function (channel, message) {
|
|
|
|
self.subClient = client;
|
|
|
|
if (channel !== channelName) {
|
|
|
|
self.subClient.subscribe(channelName);
|
|
|
|
return;
|
|
|
|
self.subClient.on('message', function (channel, message) {
|
|
|
|
}
|
|
|
|
if (channel !== channelName) {
|
|
|
|
|
|
|
|
return;
|
|
|
|
try {
|
|
|
|
}
|
|
|
|
var msg = JSON.parse(message);
|
|
|
|
|
|
|
|
self.emit(msg.event, msg.data);
|
|
|
|
try {
|
|
|
|
} catch (err) {
|
|
|
|
var msg = JSON.parse(message);
|
|
|
|
winston.error(err.stack);
|
|
|
|
self.emit(msg.event, msg.data);
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
|
|
|
|
winston.error(err.stack);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
connection.connect().then(function (client) {
|
|
|
|
|
|
|
|
self.pubClient = client;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|