|
|
@ -2,6 +2,7 @@ var SocketIO = require('socket.io'),
|
|
|
|
socketioWildcard = require('socket.io-wildcard'),
|
|
|
|
socketioWildcard = require('socket.io-wildcard'),
|
|
|
|
util = require('util'),
|
|
|
|
util = require('util'),
|
|
|
|
async = require('async'),
|
|
|
|
async = require('async'),
|
|
|
|
|
|
|
|
path = require('path'),
|
|
|
|
fs = require('fs'),
|
|
|
|
fs = require('fs'),
|
|
|
|
nconf = require('nconf'),
|
|
|
|
nconf = require('nconf'),
|
|
|
|
express = require('express'),
|
|
|
|
express = require('express'),
|
|
|
@ -30,6 +31,16 @@ Sockets.init = function() {
|
|
|
|
'browser client minification': true
|
|
|
|
'browser client minification': true
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fs.readdir(__dirname, function(err, files) {
|
|
|
|
|
|
|
|
files.splice(files.indexOf('index.js'), 1);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async.each(files, function(lib, next) {
|
|
|
|
|
|
|
|
lib = lib.slice(0, -3);
|
|
|
|
|
|
|
|
Namespaces[lib] = require('./' + lib);
|
|
|
|
|
|
|
|
next();
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
io.sockets.on('connection', function(socket) {
|
|
|
|
io.sockets.on('connection', function(socket) {
|
|
|
|
var hs = socket.handshake,
|
|
|
|
var hs = socket.handshake,
|
|
|
|
sessionID, uid, lastPostTime = 0;
|
|
|
|
sessionID, uid, lastPostTime = 0;
|
|
|
@ -123,35 +134,30 @@ Sockets.init = function() {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
socket.on('*', function(payload) {
|
|
|
|
socket.on('*', function(payload, callback) {
|
|
|
|
// Ignore all non-api messages
|
|
|
|
// Ignore all non-api messages
|
|
|
|
if (payload.name.substr(0, 4) !== 'api:') {
|
|
|
|
if (payload.name.substr(0, 4) !== 'api:') {
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
// Deconstruct the message
|
|
|
|
// Deconstruct the message
|
|
|
|
var parts = payload.name.split('.'),
|
|
|
|
var parts = payload.name.slice(4).split('.'),
|
|
|
|
namespace = parts[0],
|
|
|
|
namespace = parts[0],
|
|
|
|
command = parts[1],
|
|
|
|
command = parts[1],
|
|
|
|
subcommand = parts[2], // MUST ADD RECURSION (:P)
|
|
|
|
subcommand = parts[2], // MUST ADD RECURSION (:P)
|
|
|
|
executeHandler = function(args) {
|
|
|
|
executeHandler = function(args) {
|
|
|
|
|
|
|
|
winston.info('[socket.io] Executing: ' + payload.name);
|
|
|
|
if (!subcommand) {
|
|
|
|
if (!subcommand) {
|
|
|
|
Namespaces[namespace][command](args);
|
|
|
|
Namespaces[namespace][command].call(Namespaces[namespace], args.length ? args : callback ? callback : undefined, args.length ? callback : undefined);
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
Namespaces[namespace][command][subcommand](args);
|
|
|
|
Namespaces[namespace][command][subcommand].call(Namespaces[namespace][command], args, callback);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
if (Namespaces[namespace]) {
|
|
|
|
if (Namespaces[namespace]) {
|
|
|
|
|
|
|
|
console.log(payload);
|
|
|
|
executeHandler(payload.args);
|
|
|
|
executeHandler(payload.args);
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
fs.exists(path.join(__dirname, namespace + '.js'), function(exists) {
|
|
|
|
winston.warn('[socket.io] Unrecognized message: ' + payload.name);
|
|
|
|
if (exists) {
|
|
|
|
|
|
|
|
Namespaces[namespace] = require('./' + namespace);
|
|
|
|
|
|
|
|
executeHandler(payload.args);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
winston.warn('[socket.io] Unrecognized message: ' + payload.name);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
console.log('message!', arguments);
|
|
|
|
console.log('message!', arguments);
|
|
|
@ -251,7 +257,8 @@ function emitTopicPostStats() {
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function emitOnlineUserCount() {
|
|
|
|
Sockets.emitOnlineUserCount = emitOnlineUserCount;
|
|
|
|
|
|
|
|
function emitOnlineUserCount(callback) {
|
|
|
|
var anon = userSockets[0] ? userSockets[0].length : 0;
|
|
|
|
var anon = userSockets[0] ? userSockets[0].length : 0;
|
|
|
|
var registered = Object.keys(userSockets).length;
|
|
|
|
var registered = Object.keys(userSockets).length;
|
|
|
|
if (anon)
|
|
|
|
if (anon)
|
|
|
@ -261,7 +268,12 @@ function emitOnlineUserCount() {
|
|
|
|
users: registered + anon,
|
|
|
|
users: registered + anon,
|
|
|
|
anon: anon
|
|
|
|
anon: anon
|
|
|
|
};
|
|
|
|
};
|
|
|
|
io.sockets.emit('api:user.active.get', returnObj)
|
|
|
|
|
|
|
|
|
|
|
|
if (callback) {
|
|
|
|
|
|
|
|
callback(returnObj);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
io.sockets.emit('api:user.active.get', returnObj)
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function emitAlert(socket, title, message) {
|
|
|
|
function emitAlert(socket, title, message) {
|
|
|
|