mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-01 03:26:04 +01:00
first set of changes
This commit is contained in:
@@ -23,10 +23,13 @@ var SocketIO = require('socket.io'),
|
|||||||
/* === */
|
/* === */
|
||||||
|
|
||||||
var users = {},
|
var users = {},
|
||||||
userSockets = {},
|
|
||||||
rooms = {},
|
|
||||||
io;
|
io;
|
||||||
|
|
||||||
|
|
||||||
|
Sockets.userSockets = {};
|
||||||
|
Sockets.rooms = {};
|
||||||
|
|
||||||
|
|
||||||
Sockets.init = function() {
|
Sockets.init = function() {
|
||||||
|
|
||||||
io = socketioWildcard(SocketIO).listen(global.server, {
|
io = socketioWildcard(SocketIO).listen(global.server, {
|
||||||
@@ -35,6 +38,8 @@ Sockets.init = function() {
|
|||||||
'browser client minification': true
|
'browser client minification': true
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Sockets.server = io;
|
||||||
|
|
||||||
fs.readdir(__dirname, function(err, files) {
|
fs.readdir(__dirname, function(err, files) {
|
||||||
files.splice(files.indexOf('index.js'), 1);
|
files.splice(files.indexOf('index.js'), 1);
|
||||||
|
|
||||||
@@ -59,8 +64,10 @@ Sockets.init = function() {
|
|||||||
uid = users[sessionID] = 0;
|
uid = users[sessionID] = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
userSockets[uid] = userSockets[uid] || [];
|
socket.uid = uid;
|
||||||
userSockets[uid].push(socket);
|
|
||||||
|
Sockets.userSockets[uid] = Sockets.userSockets[uid] || [];
|
||||||
|
Sockets.userSockets[uid].push(socket);
|
||||||
|
|
||||||
/* Need to save some state for the logger & maybe some other modules later on */
|
/* Need to save some state for the logger & maybe some other modules later on */
|
||||||
socket.state = {
|
socket.state = {
|
||||||
@@ -93,14 +100,14 @@ Sockets.init = function() {
|
|||||||
|
|
||||||
socket.on('disconnect', function() {
|
socket.on('disconnect', function() {
|
||||||
|
|
||||||
var index = (userSockets[uid] || []).indexOf(socket);
|
var index = (Sockets.userSockets[uid] || []).indexOf(socket);
|
||||||
if (index !== -1) {
|
if (index !== -1) {
|
||||||
userSockets[uid].splice(index, 1);
|
Sockets.userSockets[uid].splice(index, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (userSockets[uid] && userSockets[uid].length === 0) {
|
if (Sockets.userSockets[uid] && Sockets.userSockets[uid].length === 0) {
|
||||||
delete users[sessionID];
|
delete users[sessionID];
|
||||||
delete userSockets[uid];
|
delete Sockets.userSockets[uid];
|
||||||
if (uid) {
|
if (uid) {
|
||||||
db.sortedSetRemove('users:online', uid, function(err, data) {
|
db.sortedSetRemove('users:online', uid, function(err, data) {
|
||||||
});
|
});
|
||||||
@@ -111,12 +118,12 @@ Sockets.init = function() {
|
|||||||
|
|
||||||
emitOnlineUserCount();
|
emitOnlineUserCount();
|
||||||
|
|
||||||
for (var roomName in rooms) {
|
for (var roomName in Sockets.rooms) {
|
||||||
if (rooms.hasOwnProperty(roomName)) {
|
if (Sockets.rooms.hasOwnProperty(roomName)) {
|
||||||
socket.leave(roomName);
|
socket.leave(roomName);
|
||||||
|
|
||||||
if (rooms[roomName][socket.id]) {
|
if (Sockets.rooms[roomName][socket.id]) {
|
||||||
delete rooms[roomName][socket.id];
|
delete Sockets.rooms[roomName][socket.id];
|
||||||
}
|
}
|
||||||
|
|
||||||
updateRoomBrowsingText(roomName);
|
updateRoomBrowsingText(roomName);
|
||||||
@@ -125,72 +132,48 @@ Sockets.init = function() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
socket.on('*', function(payload, callback) {
|
socket.on('*', function(payload, callback) {
|
||||||
// Ignore all non-api messages
|
function callMethod(method) {
|
||||||
if (payload.name.substr(0, 4) !== 'api:') {
|
method.call(socket, args[0]?args[0]:null, function(err, result) {
|
||||||
return;
|
if(callback) {
|
||||||
} else {
|
callback(err?{message:err.message}:null, result);
|
||||||
// Deconstruct the message
|
|
||||||
var parts = payload.name.slice(4).split('.'),
|
|
||||||
namespace = parts.slice(0, 1),
|
|
||||||
methodToCall = parts.reduce(function(prev, cur) {
|
|
||||||
if (prev !== null && prev[cur]) {
|
|
||||||
return prev[cur];
|
|
||||||
} else {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}, Namespaces);
|
|
||||||
|
|
||||||
if (methodToCall !== null) {
|
|
||||||
var sessionData = {
|
|
||||||
uid: uid,
|
|
||||||
socket: socket,
|
|
||||||
rooms: rooms,
|
|
||||||
server: io,
|
|
||||||
userSockets: userSockets
|
|
||||||
},
|
|
||||||
socketArgs = [];
|
|
||||||
|
|
||||||
// Construct the arguments that'll get passed into each socket method
|
|
||||||
if (payload.args.length) {
|
|
||||||
socketArgs = socketArgs.concat(payload.args);
|
|
||||||
}
|
}
|
||||||
if (callback !== undefined) {
|
});
|
||||||
socketArgs.push(callback);
|
}
|
||||||
}
|
|
||||||
socketArgs.push(sessionData);
|
|
||||||
|
|
||||||
// Call the requested method
|
|
||||||
if (Namespaces[namespace].before) {
|
var parts = payload.name.split('.'),
|
||||||
Namespaces[namespace].before(sessionData, function() {
|
namespace = parts.slice(0, 1),
|
||||||
try {
|
methodToCall = parts.reduce(function(prev, cur) {
|
||||||
methodToCall.apply(Namespaces, socketArgs);
|
if (prev !== null && prev[cur]) {
|
||||||
} catch (e) {
|
return prev[cur];
|
||||||
winston.error(e.message);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
} else {
|
} else {
|
||||||
try {
|
return null;
|
||||||
methodToCall.apply(Namespaces, socketArgs);
|
|
||||||
} catch (e) {
|
|
||||||
winston.error(e.message);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
// winston.info('[socket.io] Executing: ' + payload.name);
|
}, Namespaces);
|
||||||
|
|
||||||
|
if (methodToCall !== null) {
|
||||||
|
|
||||||
|
if (Namespaces[namespace].before) {
|
||||||
|
Namespaces[namespace].before(socket, function() {
|
||||||
|
callMethod(methodToCall);
|
||||||
|
});
|
||||||
} else {
|
} else {
|
||||||
winston.warn('[socket.io] Unrecognized message: ' + payload.name);
|
callMethod(methodToCall);
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
winston.warn('[socket.io] Unrecognized message: ' + payload.name);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
Sockets.logoutUser = function(uid) {
|
Sockets.logoutUser = function(uid) {
|
||||||
if(userSockets[uid] && userSockets[uid].length) {
|
if(Sockets.userSockets[uid] && Sockets.userSockets[uid].length) {
|
||||||
for(var i=0; i< userSockets[uid].length; ++i) {
|
for(var i=0; i< Sockets.userSockets[uid].length; ++i) {
|
||||||
userSockets[uid][i].emit('event:disconnect');
|
Sockets.userSockets[uid][i].emit('event:disconnect');
|
||||||
userSockets[uid][i].disconnect();
|
Sockets.userSockets[uid][i].disconnect();
|
||||||
|
|
||||||
if(!userSockets[uid]) {
|
if(!Sockets.userSockets[uid]) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -211,18 +194,18 @@ Sockets.in = function(room) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
Sockets.getConnectedClients = function() {
|
Sockets.getConnectedClients = function() {
|
||||||
return userSockets;
|
return Sockets.userSockets;
|
||||||
};
|
};
|
||||||
|
|
||||||
Sockets.getOnlineAnonCount = function () {
|
Sockets.getOnlineAnonCount = function () {
|
||||||
return userSockets[0] ? userSockets[0].length : 0;
|
return Sockets.userSockets[0] ? Sockets.userSockets[0].length : 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
/* Helpers */
|
/* Helpers */
|
||||||
|
|
||||||
Sockets.isUserOnline = isUserOnline;
|
Sockets.isUserOnline = isUserOnline;
|
||||||
function isUserOnline(uid) {
|
function isUserOnline(uid) {
|
||||||
return !!userSockets[uid] && userSockets[uid].length > 0;
|
return !!Sockets.userSockets[uid] && Sockets.userSockets[uid].length > 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
Sockets.updateRoomBrowsingText = updateRoomBrowsingText;
|
Sockets.updateRoomBrowsingText = updateRoomBrowsingText;
|
||||||
@@ -251,7 +234,7 @@ function updateRoomBrowsingText(roomName) {
|
|||||||
return anonCount;
|
return anonCount;
|
||||||
}
|
}
|
||||||
|
|
||||||
var uids = getUidsInRoom(rooms[roomName]),
|
var uids = getUidsInRoom(Sockets.rooms[roomName]),
|
||||||
anonymousCount = getAnonymousCount(roomName);
|
anonymousCount = getAnonymousCount(roomName);
|
||||||
|
|
||||||
if (uids.length === 0) {
|
if (uids.length === 0) {
|
||||||
@@ -287,8 +270,8 @@ function emitTopicPostStats(callback) {
|
|||||||
|
|
||||||
Sockets.emitOnlineUserCount = emitOnlineUserCount;
|
Sockets.emitOnlineUserCount = emitOnlineUserCount;
|
||||||
function emitOnlineUserCount(callback) {
|
function emitOnlineUserCount(callback) {
|
||||||
var anon = userSockets[0] ? userSockets[0].length : 0;
|
var anon = Sockets.userSockets[0] ? Sockets.userSockets[0].length : 0;
|
||||||
var registered = Object.keys(userSockets).length;
|
var registered = Object.keys(Sockets.userSockets).length;
|
||||||
if (anon) {
|
if (anon) {
|
||||||
registered = registered - 1;
|
registered = registered - 1;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user