Skip to content

Commit

Permalink
Revise transport Socket.io@3/4 (#6188)
Browse files Browse the repository at this point in the history
* feat :migrate socket.io 2 -> 3

* fix: backend test

* fix: ts error

* rm

* reset the test timeout

* fix: socket transports

* fix: ts

* fix: merge

* fix: merge

* resolve merge

* clean

* clean
  • Loading branch information
HMarzban committed Feb 25, 2024
1 parent 04cc3c8 commit 4887cd9
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 20 deletions.
1 change: 1 addition & 0 deletions src/node/handler/PadMessageHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -957,6 +957,7 @@ const handleClientReady = async (socket:any, message: typeof ChatMessage) => {
rev: pad.getHeadRevisionNumber(),
time: currentTime,
},
socketTransportProtocols: settings.socketTransportProtocols,
colorPalette: authorManager.getColorPalette(),
clientIp: '127.0.0.1',
userColor: authorColorId,
Expand Down
35 changes: 18 additions & 17 deletions src/node/hooks/express/socketio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,29 +70,30 @@ exports.expressCreateServer = (hookName:string, args:ArgsExpressType, cb:Functio
// there shouldn't be a browser that isn't compatible to all
// transports in this list at once
// e.g. XHR is disabled in IE by default, so in IE it should use jsonp-polling
io = new Server(args.server, {
io = new Server({
transports: settings.socketTransportProtocols,
})

function handleConnection() {
return (socket: any) => {
sockets.add(socket);
socketsEvents.emit('updated');
// https://socket.io/docs/v3/faq/index.html
const session = socket.request.session;
session.connections++;
session.save();
socket.on('disconnect', () => {
sockets.delete(socket);
socketsEvents.emit('updated');
});
};
}
io.attach(args.server, {
cookie: false,
maxHttpBufferSize: settings.socketIo.maxHttpBufferSize,
});

io.on('connection', handleConnection);
io.on('connection', (socket:any) => {
sockets.add(socket);
socketsEvents.emit('updated');
// https://socket.io/docs/v3/faq/index.html
const session = socket.request.session;
session.connections++;
session.save();
socket.on('disconnect', () => {
sockets.delete(socket);
socketsEvents.emit('updated');
});
});

io.use(exports.socketSessionMiddleware(args));

// Temporary workaround so all clients go through middleware and handle connection
io.of('/pluginfw/installer').use(exports.socketSessionMiddleware(args))
io.of('/settings').use(exports.socketSessionMiddleware(args))
Expand Down
5 changes: 3 additions & 2 deletions src/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
"security": "1.0.0",
"semver": "^7.6.0",
"socket.io": "^3.1.2",
"socket.io-client": "^4.7.4",
"socket.io-client": "^3.1.2",
"superagent": "^8.1.2",
"terser": "^5.28.1",
"threads": "^1.7.0",
Expand All @@ -88,6 +88,7 @@
"@types/sinon": "^17.0.3",
"@types/supertest": "^6.0.2",
"@types/underscore": "^1.11.15",
"cypress": "^13.6.4",
"eslint": "^8.56.0",
"eslint-config-etherpad": "^3.0.22",
"etherpad-cli-client": "^3.0.1",
Expand All @@ -113,7 +114,7 @@
},
"scripts": {
"lint": "eslint .",
"test": "mocha --import=tsx --timeout 120000 --recursive tests/backend/specs/**.ts tests/backend/specs/**/*.ts ../node_modules/ep_*/static/tests/backend/specs",
"test": "mocha --import=tsx --timeout 120000 --recursive tests/backend/specs/**.ts tests/backend/specs/**/*.ts",
"test-container": "mocha --import=tsx --timeout 5000 tests/container/specs/api",
"dev": "node --import tsx node/server.ts",
"prod": "node --import tsx node/server.ts",
Expand Down
20 changes: 19 additions & 1 deletion src/static/js/socketio.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,25 @@ const connect = (etherpadBaseUrl, namespace = '/', options = {}) => {
const baseUrl = new URL(etherpadBaseUrl, window.location);
const socketioUrl = new URL('socket.io', baseUrl);
const namespaceUrl = new URL(namespace, new URL('/', baseUrl));
return io(namespaceUrl.href, Object.assign({path: socketioUrl.pathname}, options));

let socketOptions = {
path: socketioUrl.pathname,
upgrade: true,
transports: ["websocket"]
}
socketOptions = Object.assign(options, socketOptions);

const socket = io(namespaceUrl.href, socketOptions);

socket.on('connect_error', (error) => {
if (socket.io.engine.transports.indexOf('polling') === -1) {
console.warn('WebSocket connection failed. Falling back to long-polling.');
socket.io.opts.transports = ['polling'];
socket.io.engine.upgrade = false;
}
});

return socket;
};

if (typeof exports === 'object') {
Expand Down

0 comments on commit 4887cd9

Please sign in to comment.