mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-15 18:26:15 +01:00
emit works
This commit is contained in:
@@ -90,6 +90,11 @@
|
|||||||
cxn = redis.createClient(nconf.get('redis:port'), nconf.get('redis:host'));
|
cxn = redis.createClient(nconf.get('redis:port'), nconf.get('redis:host'));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
cxn.on('error', function (err) {
|
||||||
|
winston.error(err.stack);
|
||||||
|
process.exit(1);
|
||||||
|
});
|
||||||
|
|
||||||
if (nconf.get('redis:password')) {
|
if (nconf.get('redis:password')) {
|
||||||
cxn.auth(nconf.get('redis:password'));
|
cxn.auth(nconf.get('redis:password'));
|
||||||
}
|
}
|
||||||
@@ -104,11 +109,6 @@
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
cxn.on('error', function (err) {
|
|
||||||
winston.error(err.stack);
|
|
||||||
process.exit(1);
|
|
||||||
});
|
|
||||||
|
|
||||||
return cxn;
|
return cxn;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -26,7 +26,7 @@ module.exports = function(Meta) {
|
|||||||
base: [
|
base: [
|
||||||
'public/vendor/jquery/js/jquery.js',
|
'public/vendor/jquery/js/jquery.js',
|
||||||
'public/vendor/jquery/js/jquery-ui-1.10.4.custom.js',
|
'public/vendor/jquery/js/jquery-ui-1.10.4.custom.js',
|
||||||
'./node_modules/socket.io-client/dist/socket.io.js',
|
'./node_modules/socket.io-client/socket.io.js',
|
||||||
'public/vendor/jquery/timeago/jquery.timeago.min.js',
|
'public/vendor/jquery/timeago/jquery.timeago.min.js',
|
||||||
'public/vendor/jquery/js/jquery.form.min.js',
|
'public/vendor/jquery/js/jquery.form.min.js',
|
||||||
'public/vendor/visibility/visibility.min.js',
|
'public/vendor/visibility/visibility.min.js',
|
||||||
|
|||||||
@@ -1,13 +1,13 @@
|
|||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
var SocketIO = require('socket.io'),
|
var SocketIO = require('socket.io'),
|
||||||
socketioWildcard = require('socketio-wildcard'),
|
socketioWildcard = require('socketio-wildcard')(),
|
||||||
util = require('util'),
|
util = require('util'),
|
||||||
async = require('async'),
|
async = require('async'),
|
||||||
path = require('path'),
|
path = require('path'),
|
||||||
fs = require('fs'),
|
fs = require('fs'),
|
||||||
nconf = require('nconf'),
|
nconf = require('nconf'),
|
||||||
socketCookieParser = require('cookie-parser')(nconf.get('secret')),
|
cookieParser = require('cookie-parser')(nconf.get('secret')),
|
||||||
winston = require('winston'),
|
winston = require('winston'),
|
||||||
|
|
||||||
db = require('../database'),
|
db = require('../database'),
|
||||||
@@ -66,38 +66,17 @@ function onUserDisconnect(uid, socketid, socketCount) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Sockets.init = function(server) {
|
Sockets.init = function(server) {
|
||||||
// Default socket.io config
|
|
||||||
var config = {
|
var config = {
|
||||||
log: true,
|
transports: ['polling', 'websocket'],
|
||||||
'log level': process.env.NODE_ENV === 'development' ? 2 : 1,
|
path: nconf.get('relative_path') + '/socket.io'
|
||||||
transports: ['websocket', 'xhr-polling', 'jsonp-polling', 'flashsocket'],
|
};
|
||||||
'browser client minification': true,
|
|
||||||
resource: nconf.get('relative_path') + '/socket.io'
|
|
||||||
};
|
|
||||||
|
|
||||||
// If a redis server is configured, use it as a socket.io store, otherwise, fall back to in-memory store
|
|
||||||
// if (nconf.get('redis')) {
|
|
||||||
// var RedisStore = require('socket.io/lib/stores/redis'),
|
|
||||||
// database = require('../database/redis'),
|
|
||||||
// pub = database.connect(),
|
|
||||||
// sub = database.connect(),
|
|
||||||
// client = database.connect();
|
|
||||||
|
|
||||||
// // "redis" property needs to be passed in as referenced here: https://github.com/Automattic/socket.io/issues/808
|
|
||||||
// // Probably fixed in socket.IO 1.0
|
|
||||||
// config.store = new RedisStore({
|
|
||||||
// redis: require('redis'),
|
|
||||||
// redisPub : pub,
|
|
||||||
// redisSub : sub,
|
|
||||||
// redisClient : client
|
|
||||||
// });
|
|
||||||
// } else if (nconf.get('cluster')) {
|
|
||||||
// winston.warn('[socket.io] Clustering detected, you are advised to configure Redis as a websocket store.');
|
|
||||||
// }
|
|
||||||
io = new SocketIO();
|
io = new SocketIO();
|
||||||
|
|
||||||
|
addRedisAdapter(io);
|
||||||
|
|
||||||
io.use(socketioWildcard);
|
io.use(socketioWildcard);
|
||||||
|
|
||||||
//io = socketioWildcard(SocketIO).listen(server, config);
|
|
||||||
io.listen(server, config);
|
io.listen(server, config);
|
||||||
|
|
||||||
Sockets.server = io;
|
Sockets.server = io;
|
||||||
@@ -115,91 +94,105 @@ Sockets.init = function(server) {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
io.on('connection', function(socket) {
|
io.use(function(socket, next) {
|
||||||
console.log('connection', socket.id);
|
console.log('AUTH');
|
||||||
var hs = socket.handshake,
|
|
||||||
sessionID, uid;
|
|
||||||
|
|
||||||
if (!hs) {
|
var handshake = socket.request,
|
||||||
return;
|
sessionID;
|
||||||
|
|
||||||
|
if (!handshake) {
|
||||||
|
return next(new Error('[[error:not-authorized]]'));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Validate the session, if present
|
cookieParser(handshake, {}, function(err) {
|
||||||
socketCookieParser(hs, {}, function(err) {
|
if (err) {
|
||||||
if(err) {
|
return next(err);
|
||||||
return winston.error(err.message);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
sessionID = socket.handshake.signedCookies['express.sid'];
|
var sessionID = handshake.signedCookies['express.sid'];
|
||||||
|
|
||||||
db.sessionStore.get(sessionID, function(err, sessionData) {
|
db.sessionStore.get(sessionID, function(err, sessionData) {
|
||||||
if (!err && sessionData && sessionData.passport && sessionData.passport.user) {
|
if (err) {
|
||||||
uid = parseInt(sessionData.passport.user, 10);
|
return next(err);
|
||||||
} else {
|
|
||||||
uid = 0;
|
|
||||||
}
|
}
|
||||||
|
if (sessionData && sessionData.passport && sessionData.passport.user) {
|
||||||
socket.uid = parseInt(uid, 10);
|
socket.uid = parseInt(sessionData.passport.user, 10);
|
||||||
onUserConnect(uid, socket.id);
|
|
||||||
|
|
||||||
/* If meta.config.loggerIOStatus > 0, logger.io_one will hook into this socket */
|
|
||||||
logger.io_one(socket, uid);
|
|
||||||
|
|
||||||
if (uid) {
|
|
||||||
socket.join('uid_' + uid);
|
|
||||||
socket.join('online_users');
|
|
||||||
|
|
||||||
async.parallel({
|
|
||||||
user: function(next) {
|
|
||||||
user.getUserFields(uid, ['username', 'userslug', 'picture', 'status'], next);
|
|
||||||
},
|
|
||||||
isAdmin: function(next) {
|
|
||||||
user.isAdministrator(uid, next);
|
|
||||||
}
|
|
||||||
}, function(err, userData) {
|
|
||||||
if (err || !userData.user) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
socket.emit('event:connect', {
|
|
||||||
status: 1,
|
|
||||||
username: userData.user.username,
|
|
||||||
userslug: userData.user.userslug,
|
|
||||||
picture: userData.user.picture,
|
|
||||||
isAdmin: userData.isAdmin,
|
|
||||||
uid: uid
|
|
||||||
});
|
|
||||||
|
|
||||||
socket.broadcast.emit('event:user_status_change', {uid:uid, status: userData.user.status});
|
|
||||||
});
|
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
socket.join('online_guests');
|
socket.uid = 0;
|
||||||
socket.emit('event:connect', {
|
|
||||||
status: 1,
|
|
||||||
username: '[[global:guest]]',
|
|
||||||
isAdmin: false,
|
|
||||||
uid: 0
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
next();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
io.on('connection', function(socket) {
|
||||||
|
console.log('CONNECTED', socket.uid, socket.id);
|
||||||
|
|
||||||
|
logger.io_one(socket, socket.uid);
|
||||||
|
|
||||||
|
if (socket.uid) {
|
||||||
|
onUserConnect(socket.uid, socket.id);
|
||||||
|
socket.join('uid_' + socket.uid);
|
||||||
|
socket.join('online_users');
|
||||||
|
|
||||||
|
async.parallel({
|
||||||
|
user: function(next) {
|
||||||
|
user.getUserFields(socket.uid, ['username', 'userslug', 'picture', 'status'], next);
|
||||||
|
},
|
||||||
|
isAdmin: function(next) {
|
||||||
|
user.isAdministrator(socket.uid, next);
|
||||||
|
}
|
||||||
|
}, function(err, userData) {
|
||||||
|
if (err || !userData.user) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
socket.emit('event:connect', {
|
||||||
|
status: 1,
|
||||||
|
username: userData.user.username,
|
||||||
|
userslug: userData.user.userslug,
|
||||||
|
picture: userData.user.picture,
|
||||||
|
isAdmin: userData.isAdmin,
|
||||||
|
uid: socket.uid
|
||||||
|
});
|
||||||
|
|
||||||
|
socket.broadcast.emit('event:user_status_change', {uid: socket.uid, status: userData.user.status});
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
socket.join('online_guests');
|
||||||
|
socket.emit('event:connect', {
|
||||||
|
status: 1,
|
||||||
|
username: '[[global:guest]]',
|
||||||
|
isAdmin: false,
|
||||||
|
uid: 0
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
socket.on('disconnect', function() {
|
socket.on('disconnect', function() {
|
||||||
var socketCount = Sockets.getUserSocketCount(uid);
|
var socketCount = Sockets.getUserSocketCount(socket.uid);
|
||||||
if (uid && socketCount <= 1) {
|
console.log('DISCONNECT', socket.uid, socket.id);
|
||||||
socket.broadcast.emit('event:user_status_change', {uid: uid, status: 'offline'});
|
if (socket.uid && socketCount <= 1) {
|
||||||
|
socket.broadcast.emit('event:user_status_change', {uid: socket.uid, status: 'offline'});
|
||||||
}
|
}
|
||||||
|
|
||||||
onUserDisconnect(uid, socket.id, socketCount);
|
onUserDisconnect(socket.uid, socket.id, socketCount);
|
||||||
|
|
||||||
for(var roomName in io.sockets.manager.roomClients[socket.id]) {
|
// for(var roomName in io.sockets.manager.roomClients[socket.id]) {
|
||||||
if (roomName.indexOf('topic') !== -1) {
|
// if (roomName.indexOf('topic') !== -1) {
|
||||||
io.sockets.in(roomName.slice(1)).emit('event:user_leave', socket.uid);
|
// io.sockets.in(roomName.slice(1)).emit('event:user_leave', socket.uid);
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
});
|
});
|
||||||
|
|
||||||
socket.on('*', function(payload, callback) {
|
socket.on('*', function(payload) {
|
||||||
if (!payload.name) {
|
if (!payload.data.length) {
|
||||||
|
return winston.warn('[socket.io] Empty payload');
|
||||||
|
}
|
||||||
|
|
||||||
|
var eventName = payload.data[0];
|
||||||
|
var params = payload.data[1];
|
||||||
|
var callback = typeof payload.data[payload.data.length - 1] === 'function' ? payload.data[payload.data.length - 1] : function() {};
|
||||||
|
|
||||||
|
if (!eventName) {
|
||||||
return winston.warn('[socket.io] Empty method name');
|
return winston.warn('[socket.io] Empty method name');
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -208,7 +201,7 @@ Sockets.init = function(server) {
|
|||||||
return socket.disconnect();
|
return socket.disconnect();
|
||||||
}
|
}
|
||||||
|
|
||||||
var parts = payload.name.toString().split('.'),
|
var parts = eventName.toString().split('.'),
|
||||||
namespace = parts[0],
|
namespace = parts[0],
|
||||||
methodToCall = parts.reduce(function(prev, cur) {
|
methodToCall = parts.reduce(function(prev, cur) {
|
||||||
if (prev !== null && prev[cur]) {
|
if (prev !== null && prev[cur]) {
|
||||||
@@ -220,27 +213,38 @@ Sockets.init = function(server) {
|
|||||||
|
|
||||||
if(!methodToCall) {
|
if(!methodToCall) {
|
||||||
if (process.env.NODE_ENV === 'development') {
|
if (process.env.NODE_ENV === 'development') {
|
||||||
winston.warn('[socket.io] Unrecognized message: ' + payload.name);
|
winston.warn('[socket.io] Unrecognized message: ' + eventName);
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Namespaces[namespace].before) {
|
if (Namespaces[namespace].before) {
|
||||||
Namespaces[namespace].before(socket, payload.name, function() {
|
Namespaces[namespace].before(socket, eventName, function() {
|
||||||
callMethod(methodToCall, socket, payload, callback);
|
callMethod(methodToCall, socket, params, callback);
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
callMethod(methodToCall, socket, payload, callback);
|
callMethod(methodToCall, socket, params, callback);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
function callMethod(method, socket, payload, callback) {
|
function addRedisAdapter(io) {
|
||||||
method.call(null, socket, payload.args.length ? payload.args[0] : null, function(err, result) {
|
if (nconf.get('redis')) {
|
||||||
if (callback) {
|
var redisAdapter = require('socket.io-redis');
|
||||||
callback(err ? {message: err.message} : null, result);
|
var redis = require('../database/redis');
|
||||||
}
|
var pub = redis.connect();
|
||||||
|
var sub = redis.connect();
|
||||||
|
|
||||||
|
io.adapter(redisAdapter({pubClient: pub, subClient: sub}));
|
||||||
|
} else {
|
||||||
|
winston.warn('[socket.io] Clustering detected, you are advised to configure Redis as a websocket store.');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function callMethod(method, socket, params, callback) {
|
||||||
|
method.call(null, socket, params, function(err, result) {
|
||||||
|
callback(err ? {message: err.message} : null, result);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user