node-karin 1.0.9 → 1.0.10

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/CHANGELOG.md CHANGED
@@ -1,5 +1,12 @@
1
1
  # 更新日志
2
2
 
3
+ ## [1.0.10](https://github.com/KarinJS/Karin/compare/core-v1.0.9...core-v1.0.10) (2025-01-15)
4
+
5
+
6
+ ### 🐛 Bug Fixes
7
+
8
+ * remove deprecated configuration files and enhance WebSocket client reconnection logic ([1ebec80](https://github.com/KarinJS/Karin/commit/1ebec80515fc6cc0a347b1ae06340db4c7da1054))
9
+
3
10
  ## [1.0.9](https://github.com/KarinJS/Karin/compare/core-v1.0.8...core-v1.0.9) (2025-01-15)
4
11
 
5
12
 
@@ -4,8 +4,8 @@
4
4
  },
5
5
  "ws_client": [
6
6
  {
7
- "enable": false,
8
- "url": "ws://127.0.0.1:7005",
7
+ "enable": true,
8
+ "url": "ws://127.0.0.1:7005/ws",
9
9
  "token": "123456"
10
10
  }
11
11
  ],
package/dist/index.js CHANGED
@@ -8907,6 +8907,61 @@ var init_client = __esm({
8907
8907
  init_ws2();
8908
8908
  await init_render();
8909
8909
  WebSocketClientRenderer = class extends WebSocketRender {
8910
+ /** 重连次数 */
8911
+ reconnectCount = 0;
8912
+ /** 最大重连次数,设为-1表示无限重连 */
8913
+ maxReconnectAttempts = -1;
8914
+ /** 基连延迟(ms) */
8915
+ baseDelay = 5e3;
8916
+ /** WebSocket连接URL */
8917
+ url;
8918
+ /** 连接headers */
8919
+ headers;
8920
+ constructor(socket, url, headers) {
8921
+ super(socket);
8922
+ this.url = url;
8923
+ this.headers = headers;
8924
+ }
8925
+ /**
8926
+ * @description 重连逻辑
8927
+ * @param isPrint 是否打印日志
8928
+ */
8929
+ reconnect = (isPrint = false) => {
8930
+ if (this.maxReconnectAttempts !== -1 && this.reconnectCount >= this.maxReconnectAttempts) {
8931
+ logger.error(`[render][WebSocket] \u8FDE\u63A5\u5931\u8D25\u6B21\u6570\u8FC7\u591A(${this.reconnectCount}\u6B21), \u5DF2\u505C\u6B62\u91CD\u8FDE: ${this.url}`);
8932
+ return;
8933
+ }
8934
+ this.reconnectCount++;
8935
+ const delay = this.baseDelay;
8936
+ const delaySeconds = delay / 1e3;
8937
+ if (isPrint) {
8938
+ logger.warn(`[render][WebSocket][${this.reconnectCount}] \u8FDE\u63A5\u5931\u8D25\uFF0C${delaySeconds}\u79D2\u540E\u5C06\u91CD\u8FDE: ${this.url}`);
8939
+ }
8940
+ setTimeout(() => {
8941
+ try {
8942
+ const socket = new WebSocket(this.url, { headers: this.headers });
8943
+ this.socket = socket;
8944
+ socket.once("open", async () => {
8945
+ logger.info(`[render][WebSocket] \u8FDE\u63A5\u6210\u529F: ${this.url}`);
8946
+ await this.init();
8947
+ this.reconnectCount = 0;
8948
+ });
8949
+ socket.once("error", (error) => {
8950
+ logger.debug(error);
8951
+ logger.error(`[render][WebSocket][${this.reconnectCount}] \u8FDE\u63A5\u9519\u8BEF: ${error.message}\uFF0C${delaySeconds}\u79D2\u540E\u5C06\u91CD\u8FDE: ${this.url}`);
8952
+ socket.removeAllListeners();
8953
+ socket.close();
8954
+ this.reconnect(false);
8955
+ });
8956
+ socket.once("close", () => {
8957
+ this.reconnect(true);
8958
+ });
8959
+ } catch (error) {
8960
+ logger.debug(error);
8961
+ this.reconnect();
8962
+ }
8963
+ }, delay);
8964
+ };
8910
8965
  };
8911
8966
  createWebSocketRenderClient = () => {
8912
8967
  const cfg = render();
@@ -8919,9 +8974,17 @@ var init_client = __esm({
8919
8974
  if (!enable) return;
8920
8975
  const headers = { Authorization: crypto2.createHash("md5").update(`Bearer ${token}`).digest("hex") };
8921
8976
  const socket = new WebSocket(url, { headers });
8977
+ const renderer = new WebSocketClientRenderer(socket, url, headers);
8922
8978
  socket.once("open", async () => {
8923
8979
  logger.info(`[render][WebSocket] \u8FDE\u63A5\u6210\u529F: ${url}`);
8924
- await new WebSocketClientRenderer(socket).init();
8980
+ await renderer.init();
8981
+ });
8982
+ socket.once("close", () => {
8983
+ renderer.reconnect(true);
8984
+ });
8985
+ socket.once("error", (error) => {
8986
+ logger.error(`[render][WebSocket] \u8FDE\u63A5\u9519\u8BEF: ${error.message}`);
8987
+ socket.close();
8925
8988
  });
8926
8989
  }));
8927
8990
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "node-karin",
3
- "version": "1.0.9",
3
+ "version": "1.0.10",
4
4
  "description": "Lightweight, efficient, concise, and stable robot framework.",
5
5
  "keywords": [
6
6
  "node",
@@ -1,70 +0,0 @@
1
- # 详细配置请参考: https://karin.fun/start/file
2
-
3
- # 全局默认配置
4
- default:
5
- # 群聊、频道中所有消息冷却时间,单位秒,0则无限制
6
- cd: 0
7
- # 群聊、频道中 每个人的消息冷却时间,单位秒,0则无限制。注意,开启后所有消息都会进CD,无论是否触发插件。
8
- userCD: 1
9
- # 机器人响应模式,0-所有 1-仅@机器人 2-仅回应管理员 3-仅回应别名 4-别名或@机器人 5-管理员无限制,成员别名或@机器人 6-仅回应主人
10
- mode: 0
11
-
12
- # 机器人别名 设置后别名+指令触发机器人
13
- alias:
14
- - k
15
-
16
- # 白名单插件、功能,只有在白名单中的插件、功能才会响应 `karin-plugin-test:app.js` `karin-plugin-test:测试转发`
17
- enable: []
18
-
19
- # 黑名单插件、功能,黑名单中的插件、功能不会响应 `karin-plugin-test:app.js` `karin-plugin-test:测试转发`
20
- disable: []
21
-
22
- # 群、频道成员单独黑名单
23
- memberDisable: []
24
-
25
- # 群、频道成员单独白名单
26
- memberEnable: []
27
-
28
- # 单个Bot全局配置
29
- Bot:selfId:
30
- cd: 0
31
- userCD: 1
32
- mode: 0
33
- alias: []
34
- enable: []
35
- disable: []
36
- memberDisable: []
37
- memberEnable: []
38
-
39
- # 单个Bot:单个群 配置
40
- Bot:selfId:groupId:
41
- cd: 0
42
- userCD: 1
43
- mode: 0
44
- alias: []
45
- enable: []
46
- disable: []
47
- memberDisable: []
48
- memberEnable: []
49
-
50
- # 单个Bot:单个频道 配置
51
- Bot:selfId:guildId:
52
- cd: 0
53
- userCD: 1
54
- mode: 0
55
- alias: []
56
- enable: []
57
- disable: []
58
- memberDisable: []
59
- memberEnable: []
60
-
61
- # 单个Bot:单个频道:单个子频道 配置
62
- Bot:selfId:guildId:channelId:
63
- cd: 0
64
- userCD: 1
65
- mode: 0
66
- alias: []
67
- enable: []
68
- disable: []
69
- memberDisable: []
70
- memberEnable: []
@@ -1,31 +0,0 @@
1
- # 详细配置请参考: https://karin.fun/start/file
2
-
3
- # 全局默认配置
4
- default:
5
- # 好友消息冷却时间,单位秒,0则无限制
6
- cd: 0
7
- # 机器人响应模式(私聊没有群聊那么多模式),0-所有 2-仅回应管理员 3-仅回应别名 5-管理员无限制,非管理员别名 6-仅回应主人
8
- mode: 0
9
- # 机器人别名 设置后别名+指令触发机器人
10
- alias:
11
- - k
12
- # 白名单插件、功能,只有在白名单中的插件、功能才会响应 `karin-plugin-test:app.js` `karin-plugin-test:测试转发`
13
- enable: []
14
- # 黑名单插件、功能,黑名单中的插件、功能不会响应 `karin-plugin-test:app.js` `karin-plugin-test:测试转发`
15
- disable: []
16
-
17
- # 单个Bot默认配置
18
- Bot:selfId:
19
- cd: 0
20
- mode: 0
21
- alias: []
22
- enable: []
23
- disable: []
24
-
25
- # 单个Bot:单个好友、频道成员 配置
26
- Bot:selfId:userId:
27
- cd: 0
28
- mode: 0
29
- alias: []
30
- enable: []
31
- disable: []