From 315a652b4bbfa5c74fd2cfaca0b71df1a465af00 Mon Sep 17 00:00:00 2001 From: Suwings Date: Sun, 26 Jan 2020 18:46:08 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=20=E6=9F=A5=E8=AF=A2?= =?UTF-8?q?=E9=94=99=E8=AF=AF=E6=AC=A1=E6=95=B0=E4=B8=8A=E9=99=90=E6=9C=BA?= =?UTF-8?q?=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- helper/MCPingProtocol.js | 35 ++++++++++++++++++++++++++--------- 1 file changed, 26 insertions(+), 9 deletions(-) diff --git a/helper/MCPingProtocol.js b/helper/MCPingProtocol.js index 8f82e42..a95280e 100644 --- a/helper/MCPingProtocol.js +++ b/helper/MCPingProtocol.js @@ -77,39 +77,56 @@ function formatMotd(motd) { function PingMCServer(ip, port, callback) { new MCServStatus(port, ip).getStatus().then((res) => { - callback(res); + callback(res, null); }).catch(rej => { callback(null, rej); }); } +// 任务对象数据库 const TASK_DATABASE = {}; +// 任务结果数据库 const MCPING_RESULT_DATABASE = {}; +// 任务参数数据库 +const TASK_OBJECT_DATABASE = {}; - +// 当服务器开启时,为其创建一个定时任务 function CreateMCPingTask(id, ip, port) { + // 若任务存在,禁止重复创建,每个服务器有且只有一个定时查询任务 if (TASK_DATABASE[id]) { return; } + // 每隔10秒,ping 查询一次服务器状态,并且缓存结果 const taskInterval = setInterval(() => { - console.log('正在进行查询...', ip, port); + // 任务参数对象,用于记录错误次数和其他数据 + TASK_OBJECT_DATABASE[id] = { + errorCount: 0 + } + // 进行查询 PingMCServer(ip, port, (v, e) => { - if (v) { + if (v != null && e == null) { + // 查询成功则缓存值 MCPING_RESULT_DATABASE[id] = v; + } else { + TASK_OBJECT_DATABASE[id].errorCount += 1; + // 连续查询错误次数20次以上,主动销毁自身 + if (TASK_OBJECT_DATABASE[id].errorCount > 20) { + DestroyMCPingTask(id); + } } - console.log('查询结果', v, e); }); - }, 5000); + }, 1000 * 10); TASK_DATABASE[id] = taskInterval; } +// 当服务器关闭时,为其关闭一个定时任务 function DestroyMCPingTask(id) { clearInterval(TASK_DATABASE[id]); TASK_DATABASE[id] = undefined; - delete TASK_DATABASE[id] + delete TASK_DATABASE[id]; MCPING_RESULT_DATABASE[id] = undefined; - delete MCPING_RESULT_DATABASE[id] - console.log('列表还存在:', TASK_DATABASE); + delete MCPING_RESULT_DATABASE[id]; + delete TASK_OBJECT_DATABASE[id]; } function QueryMCPingTask(id) {