koishi-plugin-disaster-warning 0.0.8 → 0.0.9

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.
package/lib/service.d.ts CHANGED
@@ -6,6 +6,7 @@ export declare class DisasterWarningService {
6
6
  private reconnectTimers;
7
7
  private pusher;
8
8
  private ctx;
9
+ private stopped;
9
10
  private handlers;
10
11
  private wolfxHandlers;
11
12
  constructor(ctx: Context, config: Config);
package/lib/service.js CHANGED
@@ -11,6 +11,7 @@ class DisasterWarningService {
11
11
  constructor(ctx, config) {
12
12
  this.connections = {};
13
13
  this.reconnectTimers = {};
14
+ this.stopped = false; // Flag to prevent reconnection after stop
14
15
  this.wolfxHandlers = {};
15
16
  this.ctx = ctx;
16
17
  this.config = config;
@@ -29,17 +30,27 @@ class DisasterWarningService {
29
30
  async start() {
30
31
  if (!this.config.enabled)
31
32
  return;
33
+ this.stopped = false; // Reset stopped flag on start
32
34
  logger.info('Disaster Warning Service starting...');
33
35
  this.connectBasedOnConfig();
34
36
  }
35
37
  async stop() {
36
38
  logger.info('Disaster Warning Service stopping...');
37
- for (const key in this.connections) {
38
- this.connections[key].close();
39
- }
39
+ this.stopped = true; // Set stopped flag to prevent reconnection
40
+ // Clear all reconnect timers first
40
41
  for (const key in this.reconnectTimers) {
41
42
  clearTimeout(this.reconnectTimers[key]);
43
+ delete this.reconnectTimers[key];
44
+ }
45
+ // Close all connections
46
+ for (const key in this.connections) {
47
+ const ws = this.connections[key];
48
+ // Remove listeners to prevent reconnection attempts
49
+ ws.removeAllListeners('close');
50
+ ws.close();
51
+ delete this.connections[key];
42
52
  }
53
+ logger.info('Disaster Warning Service stopped.');
43
54
  }
44
55
  connectBasedOnConfig() {
45
56
  const { regions, data_types, source_priority } = this.config;
@@ -90,7 +101,11 @@ class DisasterWarningService {
90
101
  }
91
102
  }
92
103
  connectWebSocket(name, url, onMessage) {
104
+ // Don't connect if service is stopped
105
+ if (this.stopped)
106
+ return;
93
107
  if (this.connections[name]) {
108
+ this.connections[name].removeAllListeners('close');
94
109
  this.connections[name].close();
95
110
  }
96
111
  logger.info(`Connecting to ${name} at ${url}...`);
@@ -108,11 +123,14 @@ class DisasterWarningService {
108
123
  }
109
124
  });
110
125
  ws.on('close', () => {
111
- logger.warn(`Disconnected from ${name}, reconnecting in 10s...`);
112
- delete this.connections[name];
113
- this.reconnectTimers[name] = setTimeout(() => {
114
- this.connectWebSocket(name, url, onMessage);
115
- }, 10000);
126
+ // Only reconnect if service is not stopped
127
+ if (!this.stopped) {
128
+ logger.warn(`Disconnected from ${name}, reconnecting in 10s...`);
129
+ delete this.connections[name];
130
+ this.reconnectTimers[name] = setTimeout(() => {
131
+ this.connectWebSocket(name, url, onMessage);
132
+ }, 10000);
133
+ }
116
134
  });
117
135
  ws.on('error', (err) => {
118
136
  logger.error(`Error in ${name} connection:`, err);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "koishi-plugin-disaster-warning",
3
- "version": "0.0.8",
3
+ "version": "0.0.9",
4
4
  "description": "Koishi 灾害预警插件,支持多数据源(地震、海啸、气象预警)",
5
5
  "contributors": [
6
6
  "lumia.wang <fenglian19980510@gmail.com>"