@alemonjs/onebot 2.1.0-alpha.6 → 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.
package/lib/index.js CHANGED
@@ -20,7 +20,7 @@ var index = () => {
20
20
  reverse_port: config?.reverse_port ?? 17158
21
21
  });
22
22
  client.connect();
23
- const url = `ws://127.0.0.1:${process.env?.port || config?.port || 17117}`;
23
+ const url = `ws://127.0.0.1:${process.env?.port || 17117}`;
24
24
  const cbp = cbpPlatform(url);
25
25
  const createUserAvatar = (id) => {
26
26
  return `https://q1.qlogo.cn/g?b=qq&s=0&nk=${id}`;
@@ -91,10 +91,6 @@ var index = () => {
91
91
  };
92
92
  cbp.send(e);
93
93
  });
94
- // 错误处理
95
- client.on('ERROR', event => {
96
- logger.error(event);
97
- });
98
94
  /**
99
95
  * @param val
100
96
  * @returns
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 事件名称
@@ -51,8 +61,7 @@ class OneBotClient extends OneBotAPI {
51
61
  try {
52
62
  const event = JSON.parse(data.toString());
53
63
  if (!event) {
54
- if (this.#events['ERROR'])
55
- this.#events['ERROR'](event);
64
+ logger.error('[OneBot] WebSocket message is empty');
56
65
  return;
57
66
  }
58
67
  else if (event?.post_type == 'meta_event') {
@@ -102,16 +111,17 @@ class OneBotClient extends OneBotAPI {
102
111
  }
103
112
  }
104
113
  catch (err) {
105
- if (this.#events['ERROR'])
106
- this.#events['ERROR'](err);
114
+ logger.error('[OneBot] WebSocket error: ', err);
107
115
  }
108
116
  };
109
117
  const onClose = (code, reason) => {
110
- if (this.#events['ERROR'])
111
- this.#events['ERROR']({
112
- de: code,
113
- reason: reason.toString('utf8')
114
- });
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);
115
125
  };
116
126
  if (!this.ws) {
117
127
  if (reverse_enable) {
@@ -123,14 +133,15 @@ class OneBotClient extends OneBotAPI {
123
133
  this.ws.on('message', onMessage);
124
134
  // close
125
135
  this.ws.on('close', onClose);
126
- console.info('connected: ws://127.0.0.1:' + reverse_port);
136
+ logger.info(`[OneBot] connected: ws://127.0.0.1:${reverse_port}`);
127
137
  });
128
138
  }
129
139
  else {
130
140
  // forward_open
131
141
  this.ws = new WebSocket(url, c);
132
142
  this.ws.on('open', () => {
133
- console.debug(`open:${url}`);
143
+ logger.info(`[OneBot] connected: ${url}`);
144
+ this.#count = 0;
134
145
  });
135
146
  // message
136
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.6",
3
+ "version": "2.1.0-alpha.8",
4
4
  "description": "onebot v11",
5
5
  "author": "lemonade",
6
6
  "license": "MIT",
@@ -21,8 +21,12 @@
21
21
  "alemonjs"
22
22
  ],
23
23
  "dependencies": {
24
+ "dayjs": "^1.11.13",
24
25
  "ws": "^8.18.1"
25
26
  },
27
+ "peerDependencies": {
28
+ "alemonjs": "^2.1.0-alpha.15"
29
+ },
26
30
  "alemonjs": {
27
31
  "desktop": {
28
32
  "platform": [