Keep original socket.io server for non-subdir clients

This commit is contained in:
mikiher 2024-11-29 04:13:00 +02:00
parent ef82e8b0d0
commit 843dd0b1b2
2 changed files with 90 additions and 76 deletions

View File

@ -84,7 +84,6 @@ class Server {
Logger.logManager = new LogManager() Logger.logManager = new LogManager()
this.server = null this.server = null
this.io = null
} }
/** /**
@ -441,18 +440,11 @@ class Server {
async stop() { async stop() {
Logger.info('=== Stopping Server ===') Logger.info('=== Stopping Server ===')
Watcher.close() Watcher.close()
Logger.info('Watcher Closed') Logger.info('[Server] Watcher Closed')
await SocketAuthority.close()
return new Promise((resolve) => { Logger.info('[Server] Closing HTTP Server')
SocketAuthority.close((err) => { await new Promise((resolve) => this.server.close(resolve))
if (err) { Logger.info('[Server] HTTP Server Closed')
Logger.error('Failed to close server', err)
} else {
Logger.info('Server successfully closed')
}
resolve()
})
})
} }
} }
module.exports = Server module.exports = Server

View File

@ -14,7 +14,7 @@ const Auth = require('./Auth')
class SocketAuthority { class SocketAuthority {
constructor() { constructor() {
this.Server = null this.Server = null
this.io = null this.socketIoServers = []
/** @type {Object.<string, SocketClient>} */ /** @type {Object.<string, SocketClient>} */
this.clients = {} this.clients = {}
@ -89,25 +89,46 @@ class SocketAuthority {
* *
* @param {Function} callback * @param {Function} callback
*/ */
close(callback) { async close() {
Logger.info('[SocketAuthority] Shutting down') Logger.info('[SocketAuthority] closing...')
// This will close all open socket connections, and also close the underlying http server const closePromises = this.socketIoServers.map((io) => {
if (this.io) this.io.close(callback) return new Promise((resolve) => {
else callback() Logger.info(`[SocketAuthority] Closing Socket.IO server: ${io.path}`)
io.close(() => {
Logger.info(`[SocketAuthority] Socket.IO server closed: ${io.path}`)
resolve()
})
})
})
await Promise.all(closePromises)
Logger.info('[SocketAuthority] closed')
this.socketIoServers = []
} }
initialize(Server) { initialize(Server) {
this.Server = Server this.Server = Server
this.io = new SocketIO.Server(this.Server.server, { const socketIoOptions = {
cors: { cors: {
origin: '*', origin: '*',
methods: ['GET', 'POST'] methods: ['GET', 'POST']
}, }
path: `${global.RouterBasePath}/socket.io` }
})
this.io.on('connection', (socket) => { const ioServer = new SocketIO.Server(Server.server, socketIoOptions)
ioServer.path = '/socket.io'
this.socketIoServers.push(ioServer)
if (global.RouterBasePath) {
// open a separate socket.io server for the router base path, keeping the original server open for legacy clients
const ioBasePath = `${global.RouterBasePath}/socket.io`
const ioBasePathServer = new SocketIO.Server(Server.server, { ...socketIoOptions, path: ioBasePath })
ioBasePathServer.path = ioBasePath
this.socketIoServers.push(ioBasePathServer)
}
this.socketIoServers.forEach((io) => {
io.on('connection', (socket) => {
this.clients[socket.id] = { this.clients[socket.id] = {
id: socket.id, id: socket.id,
socket, socket,
@ -115,7 +136,7 @@ class SocketAuthority {
} }
socket.sheepClient = this.clients[socket.id] socket.sheepClient = this.clients[socket.id]
Logger.info('[SocketAuthority] Socket Connected', socket.id) Logger.info(`[SocketAuthority] Socket Connected to ${io.path}`, socket.id)
// Required for associating a User with a socket // Required for associating a User with a socket
socket.on('auth', (token) => this.authenticateSocket(socket, token)) socket.on('auth', (token) => this.authenticateSocket(socket, token))
@ -167,6 +188,7 @@ class SocketAuthority {
socket.emit('pong') socket.emit('pong')
}) })
}) })
})
} }
/** /**