@alemonjs/onebot 2.1.0-alpha.7 → 2.1.0-alpha.8

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 (2) hide show
  1. package/lib/sdk/wss.js +17 -0
  2. package/package.json +2 -1
package/lib/sdk/wss.js CHANGED
@@ -1,5 +1,6 @@
1
1
  import WebSocket, { WebSocketServer } from 'ws';
2
2
  import { OneBotAPI, consume } from './api.js';
3
+ import dayjs from 'dayjs';
3
4
 
4
5
  /**
5
6
  * 连接
@@ -24,6 +25,15 @@ class OneBotClient extends OneBotAPI {
24
25
  }
25
26
  }
26
27
  #events = {};
28
+ // 重连次数
29
+ #count = 0;
30
+ #getReConnectTime() {
31
+ const time = this.#count > 3 ? 1000 * 6 : 1000 * 1;
32
+ const curTime = this.#count > 6 ? 1000 * this.#count * 2 : time;
33
+ logger.info(`[OneBot] 等待 ${dayjs(curTime).format('mm:ss')} 后重新连接`);
34
+ this.#count++;
35
+ return curTime;
36
+ }
27
37
  /**
28
38
  * 注册事件处理程序
29
39
  * @param key 事件名称
@@ -106,6 +116,12 @@ class OneBotClient extends OneBotAPI {
106
116
  };
107
117
  const onClose = (code, reason) => {
108
118
  logger.error(`[OneBot] WebSocket closed: ${code} - ${reason.toString('utf8')}`);
119
+ if (reverse_enable)
120
+ return;
121
+ const curTime = this.#getReConnectTime();
122
+ setTimeout(() => {
123
+ this.connect();
124
+ }, curTime);
109
125
  };
110
126
  if (!this.ws) {
111
127
  if (reverse_enable) {
@@ -125,6 +141,7 @@ class OneBotClient extends OneBotAPI {
125
141
  this.ws = new WebSocket(url, c);
126
142
  this.ws.on('open', () => {
127
143
  logger.info(`[OneBot] connected: ${url}`);
144
+ this.#count = 0;
128
145
  });
129
146
  // message
130
147
  this.ws.on('message', onMessage);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alemonjs/onebot",
3
- "version": "2.1.0-alpha.7",
3
+ "version": "2.1.0-alpha.8",
4
4
  "description": "onebot v11",
5
5
  "author": "lemonade",
6
6
  "license": "MIT",
@@ -21,6 +21,7 @@
21
21
  "alemonjs"
22
22
  ],
23
23
  "dependencies": {
24
+ "dayjs": "^1.11.13",
24
25
  "ws": "^8.18.1"
25
26
  },
26
27
  "peerDependencies": {