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