Revise transport Socket.io@3/4 (#6188)

* 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
This commit is contained in:
Hossein Marzban 2024-02-25 14:33:55 +03:30 committed by GitHub
parent 04cc3c8d54
commit 4887cd952a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 41 additions and 20 deletions

View file

@ -957,6 +957,7 @@ const handleClientReady = async (socket:any, message: typeof ChatMessage) => {
rev: pad.getHeadRevisionNumber(), rev: pad.getHeadRevisionNumber(),
time: currentTime, time: currentTime,
}, },
socketTransportProtocols: settings.socketTransportProtocols,
colorPalette: authorManager.getColorPalette(), colorPalette: authorManager.getColorPalette(),
clientIp: '127.0.0.1', clientIp: '127.0.0.1',
userColor: authorColorId, userColor: authorColorId,

View file

@ -70,12 +70,16 @@ exports.expressCreateServer = (hookName:string, args:ArgsExpressType, cb:Functio
// there shouldn't be a browser that isn't compatible to all // there shouldn't be a browser that isn't compatible to all
// transports in this list at once // transports in this list at once
// e.g. XHR is disabled in IE by default, so in IE it should use jsonp-polling // 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, transports: settings.socketTransportProtocols,
}) })
function handleConnection() { io.attach(args.server, {
return (socket: any) => { cookie: false,
maxHttpBufferSize: settings.socketIo.maxHttpBufferSize,
});
io.on('connection', (socket:any) => {
sockets.add(socket); sockets.add(socket);
socketsEvents.emit('updated'); socketsEvents.emit('updated');
// https://socket.io/docs/v3/faq/index.html // https://socket.io/docs/v3/faq/index.html
@ -86,10 +90,7 @@ exports.expressCreateServer = (hookName:string, args:ArgsExpressType, cb:Functio
sockets.delete(socket); sockets.delete(socket);
socketsEvents.emit('updated'); socketsEvents.emit('updated');
}); });
}; });
}
io.on('connection', handleConnection);
io.use(exports.socketSessionMiddleware(args)); io.use(exports.socketSessionMiddleware(args));

View file

@ -63,7 +63,7 @@
"security": "1.0.0", "security": "1.0.0",
"semver": "^7.6.0", "semver": "^7.6.0",
"socket.io": "^3.1.2", "socket.io": "^3.1.2",
"socket.io-client": "^4.7.4", "socket.io-client": "^3.1.2",
"superagent": "^8.1.2", "superagent": "^8.1.2",
"terser": "^5.28.1", "terser": "^5.28.1",
"threads": "^1.7.0", "threads": "^1.7.0",
@ -88,6 +88,7 @@
"@types/sinon": "^17.0.3", "@types/sinon": "^17.0.3",
"@types/supertest": "^6.0.2", "@types/supertest": "^6.0.2",
"@types/underscore": "^1.11.15", "@types/underscore": "^1.11.15",
"cypress": "^13.6.4",
"eslint": "^8.56.0", "eslint": "^8.56.0",
"eslint-config-etherpad": "^3.0.22", "eslint-config-etherpad": "^3.0.22",
"etherpad-cli-client": "^3.0.1", "etherpad-cli-client": "^3.0.1",
@ -113,7 +114,7 @@
}, },
"scripts": { "scripts": {
"lint": "eslint .", "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", "test-container": "mocha --import=tsx --timeout 5000 tests/container/specs/api",
"dev": "node --import tsx node/server.ts", "dev": "node --import tsx node/server.ts",
"prod": "node --import tsx node/server.ts", "prod": "node --import tsx node/server.ts",

View file

@ -19,7 +19,25 @@ const connect = (etherpadBaseUrl, namespace = '/', options = {}) => {
const baseUrl = new URL(etherpadBaseUrl, window.location); const baseUrl = new URL(etherpadBaseUrl, window.location);
const socketioUrl = new URL('socket.io', baseUrl); const socketioUrl = new URL('socket.io', baseUrl);
const namespaceUrl = new URL(namespace, new URL('/', 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') { if (typeof exports === 'object') {