This code acts as a Quake 3 server browser, connecting to a master server to retrieve a list of servers and then scanning each server for detailed information. It logs the server details and scanned information, and handles connection errors by attempting to reconnect.
npm run import -- "test quakejs master"
var importer = require('../Core')
var master = importer.import("quakejs connection")
var host = 'local.games' //'master.quakejs.com'
var port = 27950
function testServers() {
master.connect(host, port, function (err, servers) {
if (err) {
console.error(err);
// attempt to reconnect in a minute
setTimeout(testServers, 60000);
return;
}
servers.forEach(function (server) {
console.log(server);
master.scanServer(server, function (err, info) {
if (err) {
console.log('Failed to scan ' + server.addr + ':' + server.port + ', ' + err.message);
return;
}
console.log(server, info);
});
});
});
}
module.exports = testServers
const Core = require('../Core');
const { connect, scanServer, Connection } = require('quakejs');
class QuakeJSConnection {
constructor(host ='master.quakejs.com', port = 27950, reconnectTimeout = 60000) {
this.host = host;
this.port = port;
this.reconnectTimeout = reconnectTimeout;
this.connection = null;
}
async connect() {
try {
this.connection = new Connection(this.host, this.port);
return await this.connection.getConnectedServers();
} catch (err) {
console.error('Error connecting to server:', err);
await this.reconnect();
}
}
async reconnect() {
setTimeout(async () => {
await this.connect();
}, this.reconnectTimeout);
}
async scanServer(server) {
try {
const info = await this.connection.getServerInfo(server);
return { server, info };
} catch (err) {
console.log(`Failed to scan ${server.addr}:${server.port}, ${err.message}`);
}
}
async start() {
while (true) {
const servers = await this.connect();
for (const server of servers) {
console.log(server);
await this.scanServer(server);
}
await this.reconnect();
}
}
}
const quakeJSConnection = new QuakeJSConnection();
quakeJSConnection.start();
module.exports = quakeJSConnection;
This code fetches and scans information about Quake 3 servers.
Here's a breakdown:
Imports:
quakejs connection
module from ../Core
, which likely provides functions for connecting to and interacting with Quake 3 servers.Configuration:
host
and port
variables to connect to a Quake 3 master server.testServers
Function:
master.connect
.master.scanServer
for each server to retrieve detailed information.Module Export:
testServers
function, making it available to be run from other parts of the application.In essence, this code acts as a simple Quake 3 server browser, connecting to a master server, retrieving a list of servers, and then scanning each server for details.