mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-12-20 15:30:39 +01:00
feat: socket.io 3 changes (#8845)
* feat: socket.io 3 changes * feat: replace socketio-wildcard with socket.onAny up socket.io-redis to 6.x * feat: remove mongodb/psql socket.io adapters * feat: show data on fail * fix: tests * fix: typo * fix: logger test fix * fix: logger.io_close * chore: up deps * chore: update readme to reflect redis requirement * fix: increase timeout show data if test fails
This commit is contained in:
committed by
GitHub
parent
d7f5efd960
commit
1c45fa1ba5
@@ -19,27 +19,31 @@ const Sockets = module.exports;
|
||||
Sockets.init = function (server) {
|
||||
requireModules();
|
||||
|
||||
const SocketIO = require('socket.io');
|
||||
const socketioWildcard = require('socketio-wildcard')();
|
||||
const SocketIO = require('socket.io').Server;
|
||||
const io = new SocketIO({
|
||||
path: nconf.get('relative_path') + '/socket.io',
|
||||
});
|
||||
|
||||
if (nconf.get('isCluster')) {
|
||||
if (nconf.get('singleHostCluster')) {
|
||||
io.adapter(require('./single-host-cluster'));
|
||||
} else if (nconf.get('redis')) {
|
||||
// socket.io-adapter-cluster needs update
|
||||
// if (nconf.get('singleHostCluster')) {
|
||||
// io.adapter(require('./single-host-cluster'));
|
||||
// } else if (nconf.get('redis')) {
|
||||
if (nconf.get('redis')) {
|
||||
io.adapter(require('../database/redis').socketAdapter());
|
||||
} else {
|
||||
io.adapter(db.socketAdapter());
|
||||
winston.warn('clustering detected, you should setup redis!');
|
||||
}
|
||||
}
|
||||
|
||||
io.use(socketioWildcard);
|
||||
io.use(authorize);
|
||||
|
||||
io.on('connection', onConnection);
|
||||
|
||||
const opts = {
|
||||
transports: nconf.get('socket.io:transports') || ['polling', 'websocket'],
|
||||
cookie: false,
|
||||
};
|
||||
/*
|
||||
* Restrict socket.io listener to cookie domain. If none is set, infer based on url.
|
||||
* Production only so you don't get accidentally locked out.
|
||||
@@ -47,15 +51,15 @@ Sockets.init = function (server) {
|
||||
*/
|
||||
if (process.env.NODE_ENV !== 'development') {
|
||||
const origins = nconf.get('socket.io:origins');
|
||||
io.origins(origins);
|
||||
opts.cors = {
|
||||
origin: origins,
|
||||
methods: ['GET', 'POST'],
|
||||
allowedHeaders: ['content-type'],
|
||||
};
|
||||
winston.info('[socket.io] Restricting access to origin: ' + origins);
|
||||
}
|
||||
|
||||
io.listen(server, {
|
||||
transports: nconf.get('socket.io:transports'),
|
||||
cookie: false,
|
||||
});
|
||||
|
||||
io.listen(server, opts);
|
||||
Sockets.server = io;
|
||||
};
|
||||
|
||||
@@ -65,8 +69,8 @@ function onConnection(socket) {
|
||||
logger.io_one(socket, socket.uid);
|
||||
|
||||
onConnect(socket);
|
||||
|
||||
socket.on('*', function (payload) {
|
||||
socket.onAny((event, ...args) => {
|
||||
const payload = { data: [event].concat(args) };
|
||||
onMessage(socket, payload);
|
||||
});
|
||||
|
||||
@@ -89,8 +93,8 @@ function onConnect(socket) {
|
||||
}
|
||||
|
||||
socket.join('sess_' + socket.request.signedCookies[nconf.get('sessionKey')]);
|
||||
Sockets.server.sockets.sockets[socket.id].emit('checkSession', socket.uid);
|
||||
Sockets.server.sockets.sockets[socket.id].emit('setHostname', os.hostname());
|
||||
socket.emit('checkSession', socket.uid);
|
||||
socket.emit('setHostname', os.hostname());
|
||||
plugins.hooks.fire('action:sockets.connect', { socket: socket });
|
||||
}
|
||||
|
||||
@@ -225,12 +229,15 @@ Sockets.in = function (room) {
|
||||
};
|
||||
|
||||
Sockets.getUserSocketCount = function (uid) {
|
||||
return Sockets.getCountInRoom('uid_' + uid);
|
||||
};
|
||||
|
||||
Sockets.getCountInRoom = function (room) {
|
||||
if (!Sockets.server) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
const room = Sockets.server.sockets.adapter.rooms['uid_' + uid];
|
||||
return room ? room.length : 0;
|
||||
const roomMap = Sockets.server.sockets.adapter.rooms.get(room);
|
||||
return roomMap ? roomMap.size : 0;
|
||||
};
|
||||
|
||||
Sockets.warnDeprecated = (socket, replacement) => {
|
||||
|
||||
Reference in New Issue
Block a user