Polarcraft/web/server/utils/rcon.js

42 lines
750 B
JavaScript
Raw Normal View History

2023-05-06 13:09:46 +02:00
import { RCON } from 'minecraft-server-util';
const client = new RCON()
const config = useRuntimeConfig()
let connected = false;
export const sendRconCommand = async (command) => {
await connectRcon()
const message = await client.execute(command);
closeClient()
return message;
}
const connectRcon = async () => {
if (connected) return;
await client.connect(config.rconHost);
await client.login(config.rconPassword);
connected = true;
}
const debounce = (callback, wait) => {
let timeout = null;
return (...args) => {
clearTimeout(timeout);
timeout = setTimeout(() => {
callback.apply(null, args);
}, wait);
};
}
const closeClient = debounce(() => {
client.close();
connected = false;
}, 5000)