start working on websocket stuff

This commit is contained in:
Lee
2024-01-01 17:19:11 +00:00
parent 9fc087beeb
commit 15c0d2403f
5 changed files with 197 additions and 14 deletions

View File

@ -1,7 +1,9 @@
import Database from "./database/database";
import Scanner from "./scanner/scanner";
import ServerManager from "./server/serverManager";
import { formatTimestamp } from "./utils/timeUtils";
import WebsocketServer from "./websocket/websocket";
import Config from "../data/config.json";
/**
* The database instance.
@ -16,14 +18,18 @@ export const serverManager = new ServerManager();
// The scanner is responsible for scanning all servers
new Scanner();
serverManager.getServers().forEach((server) => {
const record = database.getRecord(server.getID());
if (!record) {
return;
}
console.log(
`Record for "${server.getName()}": ${record.playerCount} (${formatTimestamp(
record.timestamp
)})`
);
});
// The websocket server is responsible for
// sending data to the client in real time
new WebsocketServer(Config.websocket.port);
// serverManager.getServers().forEach((server) => {
// const record = database.getRecord(server.getID());
// if (!record) {
// return;
// }
// console.log(
// `Record for "${server.getName()}": ${record.playerCount} (${formatTimestamp(
// record.timestamp
// )})`
// );
// });

View File

@ -0,0 +1,17 @@
import { Server } from "socket.io";
import { logger } from "../utils/logger";
export default class WebsocketServer {
private server: Server;
constructor(port: number) {
logger.info(`Starting websocket server on port ${port}`);
this.server = new Server(port);
this.server.on("connection", (socket) => {
logger.info("ws: Client connected");
// todo: send ping data to client
});
}
}