@baipiaodajun/mcbots 1.2.0 → 1.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/README.md +3 -3
  2. package/package.json +1 -1
  3. package/server.js +50 -19
package/README.md CHANGED
@@ -57,7 +57,7 @@ docker run -d \
57
57
  mingli2038/mcbot:latest
58
58
  ```
59
59
 
60
- ## 🔄 新版本说明
60
+ ### 新版本说明
61
61
 
62
62
  - 新版本引入了 **热更新功能:`SERVER_JSON`**
63
63
  - 目前该功能处于 **实验状态**,可能会导致一些 **意料之外的问题**
@@ -65,7 +65,7 @@ docker run -d \
65
65
 
66
66
  ---
67
67
 
68
- ## 🛠️ 页面更新
68
+ ### 页面更新
69
69
 
70
70
  在页面底部新增了修复入口:
71
71
  **【更新 MC 机器人配置】**
@@ -74,7 +74,7 @@ docker run -d \
74
74
 
75
75
  ---
76
76
 
77
- ## 🔑 密钥设置
77
+ ### 密钥设置
78
78
 
79
79
  - 默认密钥可从 [Telegram频道](https://t.me/boost/wanjulaji) 获取
80
80
  - 也可以通过环境变量自行设置:
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@baipiaodajun/mcbots",
3
- "version": "1.2.0",
3
+ "version": "1.2.1",
4
4
  "description": "Minecraft bot and status dashboard for multi-server management",
5
5
  "main": "server.js",
6
6
  "scripts": {
package/server.js CHANGED
@@ -231,7 +231,54 @@ class MinecraftBotManager {
231
231
  status: 'initializing'
232
232
  });
233
233
  }
234
-
234
+ dispose() {
235
+ const key = `${this.host}:${this.port}`;
236
+ console.log(`[${key}] 正在释放 BotManager 所有资源...`);
237
+
238
+ // 1. 停止所有正在运行的机器人(最重要!)
239
+ this.activeBots.forEach((bot, botName) => {
240
+ try {
241
+ if (bot && typeof bot.end === 'function') {
242
+ bot.removeAllListeners(); // 关键!移除所有事件监听,防止回调残留
243
+ bot.end(); // 主动断开连接
244
+ }
245
+ console.log(`[${key}] 已断开机器人: ${botName}`);
246
+ } catch (err) {
247
+ console.warn(`[${key}] 断开 ${botName} 时出错:`, err.message);
248
+ }
249
+ });
250
+ this.activeBots.clear();
251
+ this.currentBots = 0;
252
+
253
+ // 2. 清理所有定时器(防止残留 setInterval/setTimeout)
254
+ if (this.monitoringInterval) {
255
+ clearInterval(this.monitoringInterval);
256
+ this.monitoringInterval = null;
257
+ console.log(`[${key}] 已清理 monitoringInterval`);
258
+ }
259
+
260
+ if (this.timeout) {
261
+ clearTimeout(this.timeout);
262
+ this.timeout = null;
263
+ }
264
+
265
+ if (this.resetTimer) {
266
+ clearTimeout(this.resetTimer);
267
+ this.resetTimer = null;
268
+ console.log(`[${key}] 已清理 resetTimer`);
269
+ }
270
+ // 4. 从全局状态移除(前端立刻看不到)
271
+ globalServerStatus.servers.delete(key);
272
+ console.log(`[${key}] 已从 globalServerStatus 中移除`);
273
+
274
+ // 5. 清理自身引用,帮助 GC
275
+ this.botNames = null;
276
+ this.activeBots = null;
277
+ this.status = 'disposed';
278
+ this.reachable = false;
279
+
280
+ console.log(`[${key}] BotManager 资源释放完成`);
281
+ }
235
282
  // 生成随机机器人名称
236
283
  generateBotName() {
237
284
  const baseName = this.botNames[Math.floor(Math.random() * this.botNames.length)];
@@ -1551,29 +1598,14 @@ async function hotUpdateServers() {
1551
1598
  const key = `${config.host}:${config.port}`;
1552
1599
  console.log(`热更新: 新增服务器 ${key} (${config.minBots}-${config.maxBots})`);
1553
1600
  toAdd.push(config);
1554
-
1555
- // 立即在前台状态中注册(前端马上能看到 initializing)
1556
- globalServerStatus.servers.set(key, {
1557
- host: config.host,
1558
- port: config.port,
1559
- minBots: config.minBots,
1560
- maxBots: config.maxBots,
1561
- currentBots: 0,
1562
- activeBots: [],
1563
- lastUpdate: Date.now(),
1564
- status: 'initializing'
1565
- });
1566
1601
  }
1567
1602
 
1568
1603
  // 3. 先关闭 + 移除旧的管理器
1569
1604
  for (const manager of toRemove) {
1570
- manager.stopAllBots();
1571
- if (typeof manager.stopMonitoring === 'function') {
1572
- manager.stopMonitoring();
1573
- }
1605
+ manager.dispose(); // 统一释放资源
1574
1606
  const idx = botManagers.indexOf(manager);
1575
1607
  if (idx !== -1) botManagers.splice(idx, 1);
1576
- }
1608
+ }
1577
1609
 
1578
1610
  // 4. 创建新的管理器(构造函数里会自动注册到 globalServerStatus)
1579
1611
  for (const config of toAdd) {
@@ -1587,7 +1619,6 @@ async function hotUpdateServers() {
1587
1619
  newManager.startMonitoring();
1588
1620
  botManagers.push(newManager);
1589
1621
  }
1590
-
1591
1622
  console.log(`热更新完成,当前运行服务器数量: ${botManagers.length},前端状态已同步`);
1592
1623
  }
1593
1624