fix: socket transports

This commit is contained in:
hossein 2024-02-25 13:14:27 +03:30
parent d8d96e1c86
commit 6764409448
3 changed files with 29 additions and 58 deletions

View file

@ -969,6 +969,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,

View file

@ -7,11 +7,7 @@ const express = require('../express');
const log4js = require('log4js');
const proxyaddr = require('proxy-addr');
const settings = require('../../utils/Settings');
<<<<<<< HEAD
import {Server} from 'socket.io'
=======
const {Server} = require('socket.io');
>>>>>>> 53a847ce4 (feat :migrate socket.io 2 -> 3)
const socketIORouter = require('../../handler/SocketIORouter');
const hooks = require('../../../static/js/pluginfw/hooks');
const padMessageHandler = require('../../handler/PadMessageHandler');
@ -52,15 +48,7 @@ exports.expressCloseServer = async () => {
logger.info('All socket.io clients have disconnected');
};
<<<<<<< HEAD
<<<<<<< HEAD
exports.socketSessionMiddleware = (args: any) => (socket: any, next: Function) => {
=======
exports.socketSessionMiddleware = (socket: any, next: Function) => {
>>>>>>> 53a847ce4 (feat :migrate socket.io 2 -> 3)
=======
exports.socketSessionMiddleware = (args: any) => (socket: any, next: Function) => {
>>>>>>> 152f0a1ab (fix: ts error)
const req = socket.request;
// Express sets req.ip but socket.io does not. Replicate Express's behavior here.
if (req.ip == null) {
@ -71,23 +59,9 @@ exports.socketSessionMiddleware = (args: any) => (socket: any, next: Function) =
}
}
if (!req.headers.cookie) {
<<<<<<< HEAD
<<<<<<< HEAD
// socketio.js-client on node.js doesn't support cookies, so pass them via a query parameter.
req.headers.cookie = socket.handshake.query.cookie;
}
=======
// socketio.js-client on node.js doesn't support cookies (see https://git.io/JU8u9), so the
// token and express_sid cookies have to be passed via a query parameter for unit tests.
req.headers.cookie = socket.handshake.query.cookie;
}
// See: https://socket.io/docs/faq/#Usage-with-express-session
>>>>>>> 53a847ce4 (feat :migrate socket.io 2 -> 3)
=======
// socketio.js-client on node.js doesn't support cookies, so pass them via a query parameter.
req.headers.cookie = socket.handshake.query.cookie;
}
>>>>>>> 152f0a1ab (fix: ts error)
express.sessionMiddleware(req, {}, next);
};
@ -96,15 +70,15 @@ 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,
})
<<<<<<< HEAD
function handleConnection() {
return (socket: any) => {
sockets.add(socket);
=======
io.attach(args.server, {
cookie: false,
maxHttpBufferSize: settings.socketIo.maxHttpBufferSize,
});
io.on('connection', (socket:any) => {
sockets.add(socket);
socketsEvents.emit('updated');
@ -114,35 +88,12 @@ exports.expressCreateServer = (hookName:string, args:ArgsExpressType, cb:Functio
session.save();
socket.on('disconnect', () => {
sockets.delete(socket);
>>>>>>> 53a847ce4 (feat :migrate socket.io 2 -> 3)
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');
});
};
}
<<<<<<< HEAD
<<<<<<< HEAD
io.on('connection', handleConnection);
});
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))
=======
io.use(exports.socketSessionMiddleware);
>>>>>>> 53a847ce4 (feat :migrate socket.io 2 -> 3)
=======
io.use(exports.socketSessionMiddleware(args));
>>>>>>> 152f0a1ab (fix: ts error)
io.use((socket:any, next:Function) => {
socket.conn.on('packet', (packet:string) => {
// Tell express-session that the session is still active. The session store can use these

View file

@ -19,7 +19,26 @@ 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 = {
...options,
path: socketioUrl.pathname,
upgrade: true,
transports: ["websocket"]
}
socketOptions = Object.assign(socketOptions, options);
const socket = io(namespaceUrl.href, socketOptions);
socket.on('connect_error', (error) => {
if (socket.io.engine.transports.indexOf('polling') === -1) {
console.log('WebSocket connection failed. Falling back to long-polling.');
socket.io.opts.transports = ['polling']; // Add polling transport
socket.io.engine.upgrade = false; // Disable further upgrades
}
});
return socket;
};
if (typeof exports === 'object') {