Added socketManager and moved logic into it where appropiate
This commit is contained in:
@ -1,6 +1,4 @@
|
||||
import fs from 'fs'
|
||||
import { createServer as httpServer, Server as HTTPServer } from 'http'
|
||||
import { pathToFileURL } from 'url'
|
||||
|
||||
import cors from 'cors'
|
||||
import express, { Application } from 'express'
|
||||
@ -9,12 +7,11 @@ import { Server as SocketServer } from 'socket.io'
|
||||
import config from '#application/config'
|
||||
import Database from '#application/database'
|
||||
import Logger, { LoggerType } from '#application/logger'
|
||||
import { getAppPath } from '#application/storage'
|
||||
import { TSocket } from '#application/types'
|
||||
import ConsoleManager from '#managers/consoleManager'
|
||||
import DateManager from '#managers/dateManager'
|
||||
import HttpManager from '#managers/httpManager'
|
||||
import QueueManager from '#managers/queueManager'
|
||||
import SocketManager from '#managers/socketManager'
|
||||
import UserManager from '#managers/userManager'
|
||||
import WeatherManager from '#managers/weatherManager'
|
||||
import ZoneManager from '#managers/zoneManager'
|
||||
@ -87,57 +84,8 @@ export class Server {
|
||||
// Load console manager
|
||||
await ConsoleManager.boot(this.io)
|
||||
|
||||
// Listen for socket connections
|
||||
this.io.on('connection', this.handleConnection.bind(this))
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle socket connection
|
||||
* @param socket
|
||||
* @private
|
||||
*/
|
||||
private async handleConnection(socket: TSocket) {
|
||||
try {
|
||||
await this.loadEventHandlers('events', '', socket)
|
||||
} catch (error: any) {
|
||||
this.logger.error(`Failed to load event handlers: ${error.message}`)
|
||||
}
|
||||
}
|
||||
|
||||
private async loadEventHandlers(baseDir: string, subDir: string, socket: TSocket) {
|
||||
try {
|
||||
const fullDir = getAppPath(baseDir, subDir)
|
||||
const files = await fs.promises.readdir(fullDir, { withFileTypes: true })
|
||||
|
||||
for (const file of files) {
|
||||
const filePath = getAppPath(baseDir, subDir, file.name)
|
||||
|
||||
if (file.isDirectory()) {
|
||||
await this.loadEventHandlers(baseDir, `${subDir}/${file.name}`, socket)
|
||||
continue
|
||||
}
|
||||
|
||||
if (!file.isFile() || (!file.name.endsWith('.ts') && !file.name.endsWith('.js'))) {
|
||||
continue
|
||||
}
|
||||
|
||||
try {
|
||||
const module = await import(pathToFileURL(filePath).href)
|
||||
if (typeof module.default !== 'function') {
|
||||
this.logger.warn(`Unrecognized export in ${file.name}`)
|
||||
continue
|
||||
}
|
||||
|
||||
const EventClass = module.default
|
||||
const eventInstance = new EventClass(this.io, socket)
|
||||
eventInstance.listen()
|
||||
} catch (error) {
|
||||
this.logger.error(`Error loading event handler ${file.name}: ${error instanceof Error ? error.message : String(error)}`)
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
this.logger.error(`Error reading directory: ${error instanceof Error ? error.message : String(error)}`)
|
||||
}
|
||||
// Load socket manager
|
||||
await SocketManager.boot(this.io)
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user