Use old socket io syntax.

This commit is contained in:
SamTV12345 2024-03-08 18:17:16 +01:00
parent 0db3d25986
commit 052b5057c7

View file

@ -2,12 +2,12 @@
import {ArgsExpressType} from "../../types/ArgsExpressType"; import {ArgsExpressType} from "../../types/ArgsExpressType";
const events = require('events'); import events from 'events';
const express = require('../express'); const express = require('../express');
const log4js = require('log4js'); import log4js from 'log4js';
const proxyaddr = require('proxy-addr'); const proxyaddr = require('proxy-addr');
const settings = require('../../utils/Settings'); const settings = require('../../utils/Settings');
import {Server} from 'socket.io' import {Server, Socket} from 'socket.io'
const socketIORouter = require('../../handler/SocketIORouter'); const socketIORouter = require('../../handler/SocketIORouter');
const hooks = require('../../../static/js/pluginfw/hooks'); const hooks = require('../../../static/js/pluginfw/hooks');
const padMessageHandler = require('../../handler/PadMessageHandler'); const padMessageHandler = require('../../handler/PadMessageHandler');
@ -48,7 +48,7 @@ exports.expressCloseServer = async () => {
logger.info('All socket.io clients have disconnected'); logger.info('All socket.io clients have disconnected');
}; };
exports.socketSessionMiddleware = (args: any) => (socket: any, next: Function) => { export const socketSessionMiddleware = (args: any) => (socket: any, next: Function) => {
const req = socket.request; const req = socket.request;
// Express sets req.ip but socket.io does not. Replicate Express's behavior here. // Express sets req.ip but socket.io does not. Replicate Express's behavior here.
if (req.ip == null) { if (req.ip == null) {
@ -70,14 +70,11 @@ 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({ io = new Server(args.server,{
transports: settings.socketTransportProtocols, transports: settings.socketTransportProtocols,
})
io.attach(args.server, {
cookie: false, cookie: false,
maxHttpBufferSize: settings.socketIo.maxHttpBufferSize, maxHttpBufferSize: settings.socketIo.maxHttpBufferSize,
}); })
io.on('connection', (socket:any) => { io.on('connection', (socket:any) => {
sockets.add(socket); sockets.add(socket);
@ -92,11 +89,11 @@ exports.expressCreateServer = (hookName:string, args:ArgsExpressType, cb:Functio
}); });
}); });
io.use(exports.socketSessionMiddleware(args)); io.use(socketSessionMiddleware(args));
// Temporary workaround so all clients go through middleware and handle connection // Temporary workaround so all clients go through middleware and handle connection
io.of('/pluginfw/installer').use(exports.socketSessionMiddleware(args)) io.of('/pluginfw/installer').use(socketSessionMiddleware(args))
io.of('/settings').use(exports.socketSessionMiddleware(args)) io.of('/settings').use(socketSessionMiddleware(args))
io.use((socket:any, next:Function) => { io.use((socket:any, next:Function) => {
socket.conn.on('packet', (packet:string) => { socket.conn.on('packet', (packet:string) => {