NewsBlur/node/unread_counts.js

87 lines
2.9 KiB
JavaScript
Raw Normal View History

2012-10-23 14:07:28 -07:00
// Generated by CoffeeScript 1.4.0
(function() {
2015-06-02 11:24:38 -07:00
var REDIS_SERVER, SECURE, certificate, fs, io, log, privateKey, redis;
fs = require('fs');
redis = require('redis');
2013-05-01 12:51:59 -07:00
log = require('./log.js');
2013-08-12 12:39:03 -07:00
REDIS_SERVER = process.env.NODE_ENV === 'development' ? 'localhost' : 'db_redis_pubsub';
SECURE = !!process.env.NODE_SSL;
if (SECURE) {
privateKey = fs.readFileSync('./config/certificates/newsblur.com.key').toString();
certificate = fs.readFileSync('./config/certificates/newsblur.com.crt').toString();
io = require('socket.io').listen(8889, {
key: privateKey,
2015-06-02 11:24:38 -07:00
cert: certificate
});
} else {
io = require('socket.io').listen(8888);
}
io.configure('production', function() {
2012-05-03 13:43:18 -07:00
io.set('log level', 1);
io.enable('browser client minification');
io.enable('browser client etag');
return io.enable('browser client gzip');
});
io.configure('development', function() {
return io.set('log level', 2);
});
io.sockets.on('connection', function(socket) {
2013-05-01 13:22:03 -07:00
var ip;
ip = socket.handshake.headers['X-Forwarded-For'] || socket.handshake.address.address;
socket.on('subscribe:feeds', function(feeds, username) {
var _ref,
_this = this;
this.feeds = feeds;
this.username = username;
2013-05-01 13:25:14 -07:00
log.info(this.username, ("Connecting (" + feeds.length + " feeds, " + ip + "),") + (" (" + (io.sockets.clients().length) + " users on) ") + (" " + (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.end();
}
socket.subscribe = redis.createClient(6379, REDIS_SERVER);
socket.subscribe.on("error", function(err) {
2013-08-12 11:20:00 -07:00
console.log(" ---> Error: " + err);
2013-08-12 11:22:10 -07:00
return socket.subscribe.end();
});
socket.subscribe.on("connect", function() {
socket.subscribe.subscribe(_this.feeds);
return socket.subscribe.subscribe(_this.username);
});
return socket.subscribe.on('message', function(channel, message) {
2013-05-01 12:51:59 -07:00
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);
}
});
});
return socket.on('disconnect', function() {
2013-05-01 13:22:03 -07:00
var _ref, _ref1;
if ((_ref = socket.subscribe) != null) {
_ref.end();
}
2013-05-01 12:51:59 -07:00
return log.info(this.username, ("Disconnect (" + ((_ref1 = this.feeds) != null ? _ref1.length : void 0) + " feeds, " + ip + "),") + (" there are now " + (io.sockets.clients().length - 1) + " users. ") + (" " + (SECURE ? "(SSL)" : "(non-SSL)")));
});
});
io.sockets.on('error', function(err) {
return console.log(" ---> Error (sockets): " + err);
});
}).call(this);