From 26ca8a463048faedfd15b5272114f049e1609e7a Mon Sep 17 00:00:00 2001 From: LoV432 Date: Mon, 11 Dec 2023 02:54:40 +0500 Subject: [PATCH] destroy connection after call --- app/api/get-players/route.ts | 11 +++++++++++ app/api/rcon/route.ts | 13 ++++++++++++- app/api/server-info/route.ts | 1 + app/components/ServerInfoPanel.Server.tsx | 2 ++ 4 files changed, 26 insertions(+), 1 deletion(-) diff --git a/app/api/get-players/route.ts b/app/api/get-players/route.ts index 639749b..0e4b3d0 100644 --- a/app/api/get-players/route.ts +++ b/app/api/get-players/route.ts @@ -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[] = []; @@ -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' } diff --git a/app/api/rcon/route.ts b/app/api/rcon/route.ts index fbb8bef..9090f84 100644 --- a/app/api/rcon/route.ts +++ b/app/api/rcon/route.ts @@ -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' } }); diff --git a/app/api/server-info/route.ts b/app/api/server-info/route.ts index d5caa3f..f1c2ebe 100644 --- a/app/api/server-info/route.ts +++ b/app/api/server-info/route.ts @@ -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', diff --git a/app/components/ServerInfoPanel.Server.tsx b/app/components/ServerInfoPanel.Server.tsx index e776b87..fcd2123 100644 --- a/app/components/ServerInfoPanel.Server.tsx +++ b/app/components/ServerInfoPanel.Server.tsx @@ -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 (