@alemonjs/kook 2.1.0-alpha.3 → 2.1.0

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 (40) hide show
  1. package/lib/config.d.ts +4 -5
  2. package/lib/config.js +1 -1
  3. package/lib/core/config.d.ts +8 -0
  4. package/lib/core/config.js +0 -23
  5. package/lib/desktop.d.ts +1 -3
  6. package/lib/desktop.js +0 -9
  7. package/lib/hook.d.ts +7 -19
  8. package/lib/hook.js +0 -10
  9. package/lib/index.d.ts +5 -6
  10. package/lib/index.js +396 -242
  11. package/lib/sdk/api.d.ts +3 -117
  12. package/lib/sdk/api.js +0 -112
  13. package/lib/sdk/config.d.ts +4 -0
  14. package/lib/sdk/conversation.d.ts +35 -0
  15. package/lib/sdk/conversation.js +24 -49
  16. package/lib/sdk/index.d.ts +2 -0
  17. package/lib/sdk/index.js +2 -0
  18. package/lib/sdk/instance.d.ts +3 -0
  19. package/lib/sdk/instance.js +12 -28
  20. package/lib/sdk/message/INTERACTION.d.ts +2 -0
  21. package/lib/sdk/message/INTERACTION.js +1 -0
  22. package/lib/sdk/message/MEMBER_ADD.d.ts +2 -0
  23. package/lib/sdk/message/MEMBER_ADD.js +1 -0
  24. package/lib/sdk/message/MEMBER_REMOVE.d.ts +2 -0
  25. package/lib/sdk/message/MEMBER_REMOVE.js +1 -0
  26. package/lib/sdk/message/MESSAGES_DIRECT.d.ts +2 -0
  27. package/lib/sdk/message/MESSAGES_DIRECT.js +1 -0
  28. package/lib/sdk/message/MESSAGES_PUBLIC.d.ts +2 -0
  29. package/lib/sdk/message/MESSAGES_PUBLIC.js +1 -0
  30. package/lib/sdk/message/REACTIONS.d.ts +2 -0
  31. package/lib/sdk/message/REACTIONS.js +1 -0
  32. package/lib/sdk/message.d.ts +40 -0
  33. package/lib/sdk/message.js +11 -7
  34. package/lib/sdk/typings.d.ts +192 -43
  35. package/lib/sdk/typings.js +19 -89
  36. package/lib/sdk/wss.d.ts +9 -0
  37. package/lib/sdk/wss.js +23 -40
  38. package/lib/sdk/wss.types.d.ts +3 -0
  39. package/lib/sdk/wss.types.js +1 -0
  40. package/package.json +3 -3
package/lib/sdk/wss.js CHANGED
@@ -4,38 +4,21 @@ import { KOOKAPI } from './api.js';
4
4
  import { ConversationMap } from './conversation.js';
5
5
 
6
6
  class KOOKClient extends KOOKAPI {
7
- // 标记是否已连接
8
7
  #isConnected = false;
9
- // 存储 session Id
10
8
  #sessionId = null;
11
- // 存储最新的消息序号
12
9
  #lastMessageSN = 0;
13
- /**
14
- *
15
- * @param opstion
16
- */
17
10
  constructor(opstion) {
18
11
  super();
19
12
  config.set('token', opstion.token);
20
13
  }
21
14
  #ws;
15
+ #heartbeatInterval = null;
22
16
  #events = {};
23
- /**
24
- * 注册事件处理程序
25
- * @param key 事件名称
26
- * @param val 事件处理函数
27
- */
28
17
  on(key, val) {
29
18
  this.#events[key] = val;
30
19
  return this;
31
20
  }
32
- /**
33
- * 使用获取到的网关连接地址建立 WebSocket 连接
34
- * @param token
35
- * @param conversation
36
- */
37
21
  async connect() {
38
- // 请求url
39
22
  const gatewayUrl = await this.gateway()
40
23
  .then(res => res?.data?.url)
41
24
  .catch(err => {
@@ -43,22 +26,13 @@ class KOOKClient extends KOOKAPI {
43
26
  this.#events['ERROR'](err);
44
27
  }
45
28
  });
46
- if (!gatewayUrl && gatewayUrl === '') {
29
+ if (!gatewayUrl || gatewayUrl === '') {
47
30
  return;
48
31
  }
49
- // 建立连接
50
32
  const map = {
51
33
  0: async ({ d, sn }) => {
52
- /**
53
- * 处理 EVENT 信令
54
- * 包括按序处理消息和记录最新的消息序号
55
- */
56
34
  if (d && sn) {
57
35
  if (sn === this.#lastMessageSN + 1) {
58
- /**
59
- * 消息序号正确
60
- * 按序处理消息
61
- */
62
36
  this.#lastMessageSN = sn;
63
37
  try {
64
38
  if (d.channel_type === 'GROUP') {
@@ -79,13 +53,8 @@ class KOOKClient extends KOOKAPI {
79
53
  this.#events['ERROR'](err);
80
54
  }
81
55
  }
82
- //
83
56
  }
84
57
  else if (sn > this.#lastMessageSN + 1) ;
85
- /**
86
- * 如果收到已处理过的消息序号
87
- * 则直接丢弃
88
- */
89
58
  }
90
59
  },
91
60
  1: ({ d }) => {
@@ -112,13 +81,14 @@ class KOOKClient extends KOOKAPI {
112
81
  },
113
82
  5: () => {
114
83
  console.info('[ws] Connection failed, reconnect');
115
- /**
116
- * 处理 RECONNECT 信令
117
- * 断开当前连接并进行重新连接
118
- */
119
84
  this.#isConnected = false;
120
85
  this.#sessionId = null;
121
- console.info('[ws] sessionId', this.#sessionId);
86
+ if (this.#heartbeatInterval) {
87
+ clearInterval(this.#heartbeatInterval);
88
+ this.#heartbeatInterval = null;
89
+ }
90
+ this.#ws.close();
91
+ void this.connect();
122
92
  },
123
93
  6: () => {
124
94
  console.info('[ws] resume ack');
@@ -138,8 +108,10 @@ class KOOKClient extends KOOKAPI {
138
108
  }
139
109
  };
140
110
  this.#ws.on('message', msg => void onMessage(msg));
141
- // 心跳定时发送
142
- setInterval(() => {
111
+ if (this.#heartbeatInterval) {
112
+ clearInterval(this.#heartbeatInterval);
113
+ }
114
+ this.#heartbeatInterval = setInterval(() => {
143
115
  if (this.#isConnected) {
144
116
  this.#ws.send(JSON.stringify({
145
117
  s: 2,
@@ -148,7 +120,18 @@ class KOOKClient extends KOOKAPI {
148
120
  }
149
121
  }, 30000);
150
122
  this.#ws.on('close', () => {
123
+ if (this.#heartbeatInterval) {
124
+ clearInterval(this.#heartbeatInterval);
125
+ this.#heartbeatInterval = null;
126
+ }
151
127
  console.error('[ws] close');
128
+ if (this.#isConnected) {
129
+ this.#isConnected = false;
130
+ this.#sessionId = null;
131
+ setTimeout(() => {
132
+ void this.connect();
133
+ }, 5000);
134
+ }
152
135
  });
153
136
  this.#ws.on('error', err => {
154
137
  console.error('[ws] error', err);
@@ -0,0 +1,3 @@
1
+ export interface KOOKOptions {
2
+ token: string;
3
+ }
@@ -0,0 +1 @@
1
+
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alemonjs/kook",
3
- "version": "2.1.0-alpha.3",
3
+ "version": "2.1.0",
4
4
  "description": "kook platform connection",
5
5
  "author": "lemonade",
6
6
  "license": "MIT",
@@ -8,7 +8,7 @@
8
8
  "main": "lib/index.js",
9
9
  "types": "lib",
10
10
  "scripts": {
11
- "build": "node bundle.js"
11
+ "build": "lvy build"
12
12
  },
13
13
  "exports": {
14
14
  ".": {
@@ -59,4 +59,4 @@
59
59
  "type": "git",
60
60
  "url": "https://github.com/lemonade-lab/alemonjs.git"
61
61
  }
62
- }
62
+ }