cleanup + server list on websocket connect

This commit is contained in:
Lee
2024-01-01 22:16:13 +00:00
parent 09f7b700ca
commit 9e17262804
6 changed files with 64 additions and 32 deletions

View File

@ -1,4 +1,5 @@
import { Server as SocketServer } from "socket.io";
import { Socket, Server as SocketServer } from "socket.io";
import { serverManager } from "..";
import Server, { ServerStatus } from "../server/server";
import { Ping } from "../types/ping";
import { logger } from "../utils/logger";
@ -12,11 +13,31 @@ export default class WebsocketServer {
this.server.on("connection", (socket) => {
logger.debug("ws: Client connected");
// todo: send ping data to client
this.sendServerList(socket);
});
}
/**
* Sends the server list to the given socket.
*
* @param socket the socket to send the server list to
*/
public sendServerList(socket: Socket): void {
logger.debug(`ws: Sending server list to ${socket.id}`);
const servers = [];
for (const server of serverManager.getServers()) {
servers.push({
id: server.getID(),
name: server.getName(),
ip: server.getIP(),
port: server.getPort(),
favicon: server.getFavicon(),
});
}
socket.emit("serverList", servers);
}
/**
* Sends the latest ping data for the given server to all clients.
*