NewsBlur/node/unread_counts.js
2016-11-30 12:47:20 -08:00

87 lines
2.9 KiB
JavaScript

// Generated by CoffeeScript 1.8.0
(function() {
var REDIS_SERVER, SECURE, app, certificate, fs, io, log, options, privateKey, redis;
fs = require('fs');
redis = require('redis');
log = require('./log.js');
REDIS_SERVER = process.env.NODE_ENV === 'development' ? 'localhost' : 'db_redis_pubsub';
SECURE = !!process.env.NODE_SSL;
if (SECURE) {
privateKey = fs.readFileSync('/srv/newsblur/config/certificates/newsblur.com.key').toString();
certificate = fs.readFileSync('/srv/newsblur/config/certificates/newsblur.com.crt').toString();
options = {
port: 8889,
key: privateKey,
cert: certificate
};
app = require('https').createServer(options);
app.listen(options.port);
io = require('socket.io').listen(app);
} else {
options = {
port: 8888
};
app = require('http').createServer();
app.listen(options.port);
io = require('socket.io').listen(app);
}
io.on('connection', function(socket) {
var ip;
ip = socket.handshake.headers['X-Forwarded-For'] || socket.handshake.address;
socket.on('subscribe:feeds', function(feeds, username) {
var _ref;
this.feeds = feeds;
this.username = username;
log.info(this.username, ("Connecting (" + feeds.length + " feeds, " + ip + "),") + (" (" + io.engine.clientsCount + " connected) ") + (" " + (SECURE ? "(SSL)" : "(non-SSL)")));
if (!this.username) {
return;
}
socket.on("error", function(err) {
return console.log(" ---> Error (socket): " + err);
});
if ((_ref = socket.subscribe) != null) {
_ref.quit();
}
socket.subscribe = redis.createClient(6379, REDIS_SERVER);
socket.subscribe.on("error", function(err) {
console.log(" ---> Error: " + err);
return socket.subscribe.quit();
});
socket.subscribe.on("connect", (function(_this) {
return function() {
socket.subscribe.subscribe(_this.feeds);
return socket.subscribe.subscribe(_this.username);
};
})(this));
return socket.subscribe.on('message', (function(_this) {
return function(channel, message) {
log.info(_this.username, "Update on " + channel + ": " + message);
if (channel === _this.username) {
return socket.emit('user:update', channel, message);
} else {
return socket.emit('feed:update', channel, message);
}
};
})(this));
});
return socket.on('disconnect', function() {
var _ref, _ref1;
if ((_ref = socket.subscribe) != null) {
_ref.quit();
}
return log.info(this.username, ("Disconnect (" + ((_ref1 = this.feeds) != null ? _ref1.length : void 0) + " feeds, " + ip + "),") + (" there are now " + io.engine.clientsCount + " users. ") + (" " + (SECURE ? "(SSL)" : "(non-SSL)")));
});
});
io.sockets.on('error', function(err) {
return console.log(" ---> Error (sockets): " + err);
});
}).call(this);