mirror of
https://github.com/ether/etherpad-lite.git
synced 2025-04-22 16:36:15 -04:00
Migrate Socket.IO from Version 2 to Version 3 🚀 (#6152)
* feat :migrate socket.io 2 -> 3 * fix: backend test * fix: ts error * rm * reset the test timeout * Updated cli client. * Updated lock file. * Use updated load tester. --------- Co-authored-by: SamTV12345 <40429738+samtv12345@users.noreply.github.com>
This commit is contained in:
parent
47f0113fd7
commit
b2be2ca714
17 changed files with 265 additions and 321 deletions
|
@ -83,7 +83,7 @@ exports.socketio = () => {
|
|||
const sessioninfos = {};
|
||||
exports.sessioninfos = sessioninfos;
|
||||
|
||||
stats.gauge('totalUsers', () => socketio ? Object.keys(socketio.sockets.sockets).length : 0);
|
||||
stats.gauge('totalUsers', () => socketio ? socketio.sockets.size : 0);
|
||||
stats.gauge('activePads', () => {
|
||||
const padIds = new Set();
|
||||
for (const {padId} of Object.values(sessioninfos)) {
|
||||
|
@ -155,13 +155,13 @@ exports.handleConnect = (socket) => {
|
|||
* Kicks all sessions from a pad
|
||||
*/
|
||||
exports.kickSessionsFromPad = (padID) => {
|
||||
if (typeof socketio.sockets.clients !== 'function') return;
|
||||
if (typeof socketio.sockets.clients !== 'object') return;
|
||||
|
||||
// skip if there is nobody on this pad
|
||||
if (_getRoomSockets(padID).length === 0) return;
|
||||
|
||||
// disconnect everyone from this pad
|
||||
socketio.sockets.in(padID).json.send({disconnect: 'deleted'});
|
||||
socketio.in(padID).emit('message', {disconnect: 'deleted'});
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -183,7 +183,7 @@ exports.handleDisconnect = async (socket) => {
|
|||
` authorID:${session.author}` +
|
||||
(user && user.username ? ` username:${user.username}` : ''));
|
||||
/* eslint-enable prefer-template */
|
||||
socket.broadcast.to(session.padId).json.send({
|
||||
socket.broadcast.to(session.padId).emit('message', {
|
||||
type: 'COLLABROOM',
|
||||
data: {
|
||||
type: 'USER_LEAVE',
|
||||
|
@ -216,7 +216,7 @@ exports.handleMessage = async (socket, message) => {
|
|||
messageLogger.warn(`Rate limited IP ${socket.request.ip}. To reduce the amount of rate ` +
|
||||
'limiting that happens edit the rateLimit values in settings.json');
|
||||
stats.meter('rateLimited').mark();
|
||||
socket.json.send({disconnect: 'rateLimited'});
|
||||
socket.emit('message', {disconnect: 'rateLimited'});
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
@ -267,11 +267,11 @@ exports.handleMessage = async (socket, message) => {
|
|||
const {accessStatus, authorID} =
|
||||
await securityManager.checkAccess(auth.padID, auth.sessionID, auth.token, user);
|
||||
if (accessStatus !== 'grant') {
|
||||
socket.json.send({accessStatus});
|
||||
socket.emit('message', {accessStatus});
|
||||
throw new Error('access denied');
|
||||
}
|
||||
if (thisSession.author != null && thisSession.author !== authorID) {
|
||||
socket.json.send({disconnect: 'rejected'});
|
||||
socket.emit('message', {disconnect: 'rejected'});
|
||||
throw new Error([
|
||||
'Author ID changed mid-session. Bad or missing token or sessionID?',
|
||||
`socket:${socket.id}`,
|
||||
|
@ -393,10 +393,10 @@ exports.handleCustomObjectMessage = (msg, sessionID) => {
|
|||
if (msg.data.type === 'CUSTOM') {
|
||||
if (sessionID) {
|
||||
// a sessionID is targeted: directly to this sessionID
|
||||
socketio.sockets.socket(sessionID).json.send(msg);
|
||||
socketio.sockets.socket(sessionID).emit('message', msg);
|
||||
} else {
|
||||
// broadcast to all clients on this pad
|
||||
socketio.sockets.in(msg.data.payload.padId).json.send(msg);
|
||||
socketio.sockets.in(msg.data.payload.padId).emit('message', msg);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
@ -416,7 +416,7 @@ exports.handleCustomMessage = (padID, msgString) => {
|
|||
time,
|
||||
},
|
||||
};
|
||||
socketio.sockets.in(padID).json.send(msg);
|
||||
socketio.sockets.in(padID).emit('message', msg);
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -453,7 +453,7 @@ exports.sendChatMessageToPadClients = async (mt, puId, text = null, padId = null
|
|||
// authorManager.getAuthorName() to resolve before saving the message to the database.
|
||||
const promise = pad.appendChatMessage(message);
|
||||
message.displayName = await authorManager.getAuthorName(message.authorId);
|
||||
socketio.sockets.in(padId).json.send({
|
||||
socketio.sockets.in(padId).emit('message', {
|
||||
type: 'COLLABROOM',
|
||||
data: {type: 'CHAT_MESSAGE', message},
|
||||
});
|
||||
|
@ -483,7 +483,7 @@ const handleGetChatMessages = async (socket, {data: {start, end}}) => {
|
|||
};
|
||||
|
||||
// send the messages back to the client
|
||||
socket.json.send(infoMsg);
|
||||
socket.emit('message', infoMsg);
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -500,7 +500,7 @@ const handleSuggestUserName = (socket, message) => {
|
|||
_getRoomSockets(padId).forEach((socket) => {
|
||||
const session = sessioninfos[socket.id];
|
||||
if (session && session.author === unnamedId) {
|
||||
socket.json.send(message);
|
||||
socket.emit('message', message);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
@ -539,7 +539,7 @@ const handleUserInfoUpdate = async (socket, {data: {userInfo: {name, colorId}}})
|
|||
};
|
||||
|
||||
// Send the other clients on the pad the update message
|
||||
socket.broadcast.to(padId).json.send(infoMsg);
|
||||
socket.broadcast.to(padId).emit('message',infoMsg);
|
||||
|
||||
// Block until the authorManager has stored the new attributes.
|
||||
await p;
|
||||
|
@ -654,12 +654,12 @@ const handleUserChanges = async (socket, message) => {
|
|||
// The client assumes that ACCEPT_COMMIT and NEW_CHANGES messages arrive in order. Make sure we
|
||||
// have already sent any previous ACCEPT_COMMIT and NEW_CHANGES messages.
|
||||
assert.equal(thisSession.rev, r);
|
||||
socket.json.send({type: 'COLLABROOM', data: {type: 'ACCEPT_COMMIT', newRev}});
|
||||
socket.emit('message', {type: 'COLLABROOM', data: {type: 'ACCEPT_COMMIT', newRev}});
|
||||
thisSession.rev = newRev;
|
||||
if (newRev !== r) thisSession.time = await pad.getRevisionDate(newRev);
|
||||
await exports.updatePadClients(pad);
|
||||
} catch (err) {
|
||||
socket.json.send({disconnect: 'badChangeset'});
|
||||
socket.emit('message', {disconnect: 'badChangeset'});
|
||||
stats.meter('failedChangesets').mark();
|
||||
messageLogger.warn(`Failed to apply USER_CHANGES from author ${thisSession.author} ` +
|
||||
`(socket ${socket.id}) on pad ${thisSession.padId}: ${err.stack || err}`);
|
||||
|
@ -716,7 +716,7 @@ exports.updatePadClients = async (pad) => {
|
|||
},
|
||||
};
|
||||
try {
|
||||
socket.json.send(msg);
|
||||
socket.emit('message', msg);
|
||||
} catch (err) {
|
||||
messageLogger.error(`Failed to notify user of new revision: ${err.stack || err}`);
|
||||
return;
|
||||
|
@ -833,7 +833,7 @@ const handleClientReady = async (socket, message) => {
|
|||
// fix user's counter, works on page refresh or if user closes browser window and then rejoins
|
||||
sessioninfos[otherSocket.id] = {};
|
||||
otherSocket.leave(sessionInfo.padId);
|
||||
otherSocket.json.send({disconnect: 'userdup'});
|
||||
otherSocket.emit('message', {disconnect: 'userdup'});
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -899,7 +899,7 @@ const handleClientReady = async (socket, message) => {
|
|||
apool: forWire.pool,
|
||||
author: changesets[r].author,
|
||||
currentTime: changesets[r].timestamp}};
|
||||
socket.json.send(wireMsg);
|
||||
socket.emit('message', wireMsg);
|
||||
}
|
||||
|
||||
if (startNum === endNum) {
|
||||
|
@ -907,7 +907,7 @@ const handleClientReady = async (socket, message) => {
|
|||
data: {type: 'CLIENT_RECONNECT',
|
||||
noChanges: true,
|
||||
newRev: pad.getHeadRevisionNumber()}};
|
||||
socket.json.send(Msg);
|
||||
socket.emit('message', Msg);
|
||||
}
|
||||
} else {
|
||||
// This is a normal first connect
|
||||
|
@ -921,7 +921,7 @@ const handleClientReady = async (socket, message) => {
|
|||
atext.attribs = attribsForWire.translated;
|
||||
} catch (e) {
|
||||
messageLogger.error(e.stack || e);
|
||||
socket.json.send({disconnect: 'corruptPad'}); // pull the brakes
|
||||
socket.emit('message', {disconnect: 'corruptPad'}); // pull the brakes
|
||||
throw new Error('corrupt pad');
|
||||
}
|
||||
|
||||
|
@ -1005,14 +1005,14 @@ const handleClientReady = async (socket, message) => {
|
|||
socket.join(sessionInfo.padId);
|
||||
|
||||
// Send the clientVars to the Client
|
||||
socket.json.send({type: 'CLIENT_VARS', data: clientVars});
|
||||
socket.emit('message', {type: 'CLIENT_VARS', data: clientVars});
|
||||
|
||||
// Save the current revision in sessioninfos, should be the same as in clientVars
|
||||
sessionInfo.rev = pad.getHeadRevisionNumber();
|
||||
}
|
||||
|
||||
// Notify other users about this new user.
|
||||
socket.broadcast.to(sessionInfo.padId).json.send({
|
||||
socket.broadcast.to(sessionInfo.padId).emit('message', {
|
||||
type: 'COLLABROOM',
|
||||
data: {
|
||||
type: 'USER_NEWINFO',
|
||||
|
@ -1062,7 +1062,7 @@ const handleClientReady = async (socket, message) => {
|
|||
},
|
||||
};
|
||||
|
||||
socket.json.send(msg);
|
||||
socket.emit('message', msg);
|
||||
}));
|
||||
|
||||
await hooks.aCallAll('userJoin', {
|
||||
|
@ -1092,7 +1092,7 @@ const handleChangesetRequest = async (socket, {data: {granularity, start, reques
|
|||
start = headRev;
|
||||
const data = await getChangesetInfo(pad, start, end, granularity);
|
||||
data.requestID = requestID;
|
||||
socket.json.send({type: 'CHANGESET_REQ', data});
|
||||
socket.emit('message', {type: 'CHANGESET_REQ', data});
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -1236,13 +1236,16 @@ const composePadChangesets = async (pad, startNum, endNum) => {
|
|||
|
||||
const _getRoomSockets = (padID) => {
|
||||
const ns = socketio.sockets; // Default namespace.
|
||||
const adapter = ns.adapter;
|
||||
// We could call adapter.clients(), but that method is unnecessarily asynchronous. Replicate what
|
||||
// it does here, but synchronously to avoid a race condition. This code will have to change when
|
||||
// we update to socket.io v3.
|
||||
const room = adapter.rooms[padID];
|
||||
const room = ns.adapter.rooms?.get(padID);
|
||||
|
||||
if (!room) return [];
|
||||
return Object.keys(room.sockets).map((id) => ns.connected[id]).filter((s) => s);
|
||||
|
||||
return Array.from(room)
|
||||
.map(socketId => ns.sockets.get(socketId))
|
||||
.filter(socket => socket);
|
||||
};
|
||||
|
||||
/**
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue