import type { Server } from './Server'; import { Connection } from './adapter/Connection'; import { ConnectionController } from './ConnectionController'; import { AnswerError } from '../messages/AnswerError'; export class ServerController { public readonly clientConnections = new Set(); constructor(private readonly server: Server) { } public async onClientConnected(connection: Connection): Promise { const controller = new ConnectionController(connection, this.server); try { await controller.init(); this.clientConnections.add(controller); } catch (e: any) { const result = e instanceof AnswerError ? e : new AnswerError('UnknownError', e); await controller.send({ type: 'fail', success: false, result, }); await connection.close('ERROR'); } return controller; } public onClientDisconnected(cm: ConnectionController) { this.clientConnections.delete(cm); } public getServer(): Server { return this.server; } }