async listen() {
this.server = new Server({
hostname: this.address,
port: this.port,
chunkSize: MAX_BYTES_ALLOWED
});
this.server.on("listen", () => Logger.log(Level.INFO, `Listening on ${this.address}:${this.port}`));
this.server.on("connect", (client: Client) => {
const connection = new ClientConnection(client);
this.connections.set(client, connection);
Logger.log(Level.INFO, `Client ${connection.id} connected!`);
client.addListener("receive", async (_, packet: Packet) => {
const reader = new ByteReader(packet.data);
reader.read(Type.VAR_INT) as number; // TODO: packet length?
const packet_id = reader.read(Type.VAR_INT) as number;
await connection.handle(this, reader, packet, packet_id);
});
client.addListener("close", async () => {
Logger.log(Level.INFO, `Client ${connection.id} disconnected!`);
// TODO: cleanup?
await connection.close();
this.connections.delete(client);
});
});
await this.server.listen();
}