cleanup + server list on websocket connect
This commit is contained in:
parent
09f7b700ca
commit
9e17262804
@ -64,7 +64,7 @@ export default class Database {
|
||||
|
||||
constructor() {
|
||||
this.db = new SQLiteDatabase(`${DATA_DIR}/db.sqlite`);
|
||||
this.db.pragma("journal_mode = WAL");
|
||||
this.db.pragma("journal_mode = WAL"); // Enable WAL mode for better performance
|
||||
|
||||
logger.info("Ensuring tables exist");
|
||||
this.db.exec(CREATE_PINGS_TABLE); // Ensure the pings table exists
|
||||
|
@ -20,8 +20,12 @@ export const serverManager = new ServerManager();
|
||||
*/
|
||||
export const websocketServer = new WebsocketServer(Config.websocket.port);
|
||||
|
||||
// The scanner is responsible for scanning all servers
|
||||
new Scanner();
|
||||
(async () => {
|
||||
await serverManager.init();
|
||||
|
||||
// The scanner is responsible for scanning all servers
|
||||
new Scanner();
|
||||
})();
|
||||
|
||||
// The websocket server is responsible for
|
||||
// sending data to the client in real time
|
||||
|
@ -9,7 +9,6 @@ export default class Scanner {
|
||||
constructor() {
|
||||
logger.info("Loading scanner database");
|
||||
|
||||
logger.info("Starting server scan");
|
||||
cron.schedule(Config.scanner.updateCron, () => {
|
||||
this.scanServers();
|
||||
});
|
||||
@ -41,7 +40,7 @@ export default class Scanner {
|
||||
let online = false;
|
||||
|
||||
try {
|
||||
response = await server.pingServer(server);
|
||||
response = await server.pingServer();
|
||||
if (response == undefined) {
|
||||
return; // Server is offline
|
||||
}
|
||||
|
@ -80,19 +80,20 @@ export default class Server {
|
||||
* Pings a server and gets the response.
|
||||
*
|
||||
* @param server the server to ping
|
||||
* @param insertPing whether to insert the ping into the database
|
||||
* @returns the ping response or undefined if the server is offline
|
||||
*/
|
||||
public pingServer(server: Server): Promise<Ping | undefined> {
|
||||
switch (server.getType()) {
|
||||
public pingServer(): Promise<Ping | undefined> {
|
||||
switch (this.getType()) {
|
||||
case "PC": {
|
||||
return this.pingPCServer(server);
|
||||
return this.pingPCServer();
|
||||
}
|
||||
case "PE": {
|
||||
return this.pingPEServer(server);
|
||||
return this.pingPEServer();
|
||||
}
|
||||
default: {
|
||||
throw new Error(
|
||||
`Unknown server type ${server.getType()} for ${server.getName()}`
|
||||
`Unknown server type ${this.getType()} for ${this.getName()}`
|
||||
);
|
||||
}
|
||||
}
|
||||
@ -104,10 +105,10 @@ export default class Server {
|
||||
* @param server the server to ping
|
||||
* @returns the ping response or undefined if the server is offline
|
||||
*/
|
||||
private async pingPCServer(server: Server): Promise<Ping | undefined> {
|
||||
private async pingPCServer(): Promise<Ping | undefined> {
|
||||
if (this.dnsInfo.resolvedServer == undefined && !this.dnsInfo.hasResolved) {
|
||||
try {
|
||||
const resolvedServer = await resolveDns(server.getIP());
|
||||
const resolvedServer = await resolveDns(this.getIP());
|
||||
|
||||
this.dnsInfo = {
|
||||
hasResolved: true,
|
||||
@ -125,7 +126,7 @@ export default class Server {
|
||||
ip = resolvedServer.ip;
|
||||
port = resolvedServer.port;
|
||||
} else {
|
||||
ip = server.getIP();
|
||||
ip = this.getIP();
|
||||
port = 25565; // The default port
|
||||
}
|
||||
|
||||
@ -139,7 +140,7 @@ export default class Server {
|
||||
|
||||
this.favicon = res.favicon; // Set the favicon
|
||||
resolve({
|
||||
id: server.getID(),
|
||||
id: this.getID(),
|
||||
timestamp: Date.now(),
|
||||
ip: ip,
|
||||
playerCount: res.players.online,
|
||||
@ -154,20 +155,20 @@ export default class Server {
|
||||
* @param server the server to ping
|
||||
* @returns the ping response or undefined if the server is offline
|
||||
*/
|
||||
private async pingPEServer(server: Server): Promise<Ping | undefined> {
|
||||
private async pingPEServer(): Promise<Ping | undefined> {
|
||||
return new Promise((resolve, reject) => {
|
||||
bedrockPing(
|
||||
server.getIP(),
|
||||
server.getPort() || 19132,
|
||||
this.getIP(),
|
||||
this.getPort() || 19132,
|
||||
(err: any, res: any) => {
|
||||
if (err || res == undefined) {
|
||||
return reject(err);
|
||||
}
|
||||
|
||||
resolve({
|
||||
id: server.getID(),
|
||||
id: this.getID(),
|
||||
timestamp: Date.now(),
|
||||
ip: server.getIP(),
|
||||
ip: this.getIP(),
|
||||
playerCount: res.currentPlayers,
|
||||
});
|
||||
}
|
||||
|
@ -5,16 +5,23 @@ import Servers from "../../data/servers.json";
|
||||
export default class ServerManager {
|
||||
private servers: Server[] = [];
|
||||
|
||||
constructor() {
|
||||
for (const server of Servers) {
|
||||
this.servers.push(
|
||||
new Server({
|
||||
id: server.id,
|
||||
ip: server.ip,
|
||||
name: server.name,
|
||||
type: server.type as ServerType,
|
||||
})
|
||||
);
|
||||
constructor() {}
|
||||
|
||||
/**
|
||||
* Loads the servers from the config file.
|
||||
*/
|
||||
async init() {
|
||||
for (const configServer of Servers) {
|
||||
const server = new Server({
|
||||
id: configServer.id,
|
||||
ip: configServer.ip,
|
||||
name: configServer.name,
|
||||
type: configServer.type as ServerType,
|
||||
});
|
||||
try {
|
||||
await server.pingServer();
|
||||
} catch (err) {}
|
||||
this.servers.push(server);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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.
|
||||
*
|
||||
|
Loading…
x
Reference in New Issue
Block a user