Skip to content

Commit

Permalink
destroy connection after call
Browse files Browse the repository at this point in the history
  • Loading branch information
LoV432 committed Dec 10, 2023
1 parent f991eac commit 26ca8a4
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 1 deletion.
11 changes: 11 additions & 0 deletions app/api/get-players/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@ export const dynamic = 'force-dynamic';
import { rconInit } from '@/app/lib/rcon';
import { parsePlayerData, Player } from '@/app/lib/parse-players';

// const rcon = await rconInit();
// rcon.on('disconnect', async (reason) => {
// // console.log('disconnected', reason);
// try {
// await rcon.reconnect();
// } catch (e: any) {
// // console.log('reconnect failed', e.message);
// }
// });

let lastReqTime = 0;
let playersData: Player[] = [];

Expand All @@ -14,6 +24,7 @@ export async function GET() {
lastReqTime = Date.now();
const rcon = await rconInit();
const rconRes = await rcon.exec('status');
rcon.destroy();
playersData = await parsePlayerData(rconRes);
return new Response(JSON.stringify(playersData), {
headers: { 'Content-Type': 'application/json' }
Expand Down
13 changes: 12 additions & 1 deletion app/api/rcon/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,25 @@ export const dynamic = 'force-dynamic';
import { NextRequest } from 'next/server';
import { rconInit } from '@/app/lib/rcon';

// const rcon = await rconInit();
// rcon.on('disconnect', async (reason) => {
// // console.log('disconnected', reason);
// try {
// await rcon.reconnect();
// } catch (e: any) {
// // console.log('reconnect failed', e.message);
// }
// });

export async function POST(request: NextRequest) {
const rcon = await rconInit();
const body = await request.json();
if (!body.command || body.command.trim().length === 0) {
return new Response('Command is required', { status: 400 });
}

const rcon = await rconInit();
const res = await rcon.exec(body.command);
rcon.destroy();
return new Response(res, {
headers: { 'Content-Type': 'text/plain' }
});
Expand Down
1 change: 1 addition & 0 deletions app/api/server-info/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export async function GET() {
lastReqTime = Date.now();
const csServer = await csServerInit();
serverInfo = await csServer.getInfo();
csServer.disconnect();
return new Response(JSON.stringify(serverInfo), {
headers: {
'Content-Type': 'application/json',
Expand Down
2 changes: 2 additions & 0 deletions app/components/ServerInfoPanel.Server.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ export default async function ServerInfoPanel() {
const players = await rcon.exec('status');
const allPlayers = await parsePlayerData(players);
const maxMindIsEnabled = process.env.MAXMIND_LICENSE_KEY ? true : false;
rcon.destroy();
csServer.disconnect();
return (
<div className="m-5 h-fit rounded-md bg-zinc-800 p-4">
<ServerInfo serverInfoPreRender={serverInfo} />
Expand Down

0 comments on commit 26ca8a4

Please sign in to comment.