douyin-danma-listener 0.2.1 → 0.3.1
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/README.md +2 -1
- package/lib/index.js +13 -2
- package/package.json +1 -1
- package/types/types.d.ts +2 -0
package/README.md
CHANGED
|
@@ -28,10 +28,11 @@ client.connect();
|
|
|
28
28
|
配置项如下:
|
|
29
29
|
|
|
30
30
|
- `autoStart` (boolean): 是否自动开始连接,默认为 `false`
|
|
31
|
-
- `autoReconnect` (number): 自动重连次数,默认为 `
|
|
31
|
+
- `autoReconnect` (number): 自动重连次数,默认为 `10`
|
|
32
32
|
- `heartbeatInterval` (number): 心跳包发送间隔,单位为毫秒,默认为 `10000`
|
|
33
33
|
- `cookie` (string): 可选的 Cookie 字符串,某些直播间可能需要?
|
|
34
34
|
- `timeoutInterval` (number): 没有数据返回但`ws`未被主动关闭时超时后重新连接,单位为秒,默认`100`
|
|
35
|
+
- `reconnectInterval`: 重连等待时间
|
|
35
36
|
|
|
36
37
|
## 事件
|
|
37
38
|
|
package/lib/index.js
CHANGED
|
@@ -26,11 +26,13 @@ class DouYinDanmaClient extends TypedEmitter {
|
|
|
26
26
|
autoStart;
|
|
27
27
|
autoReconnect;
|
|
28
28
|
reconnectAttempts;
|
|
29
|
+
reconnectInterval;
|
|
29
30
|
cookie;
|
|
30
31
|
timeoutInterval;
|
|
31
32
|
lastMessageTime;
|
|
32
33
|
timeoutTimer;
|
|
33
34
|
isTimeoutCheckRunning = false;
|
|
35
|
+
isReconnecting = false;
|
|
34
36
|
constructor(roomId, options = {}) {
|
|
35
37
|
super();
|
|
36
38
|
this.roomId = roomId;
|
|
@@ -38,6 +40,7 @@ class DouYinDanmaClient extends TypedEmitter {
|
|
|
38
40
|
this.autoStart = options.autoStart ?? false;
|
|
39
41
|
this.autoReconnect = options.autoReconnect ?? 10;
|
|
40
42
|
this.reconnectAttempts = 0;
|
|
43
|
+
this.reconnectInterval = options.reconnectInterval ?? 10000;
|
|
41
44
|
this.cookie = options.cookie;
|
|
42
45
|
this.timeoutInterval = options.timeoutInterval ?? 100000; // 默认100秒
|
|
43
46
|
this.lastMessageTime = Date.now();
|
|
@@ -59,6 +62,7 @@ class DouYinDanmaClient extends TypedEmitter {
|
|
|
59
62
|
},
|
|
60
63
|
});
|
|
61
64
|
this.ws.on("open", () => {
|
|
65
|
+
this.reconnectAttempts = 0;
|
|
62
66
|
this.emit("open");
|
|
63
67
|
this.startHeartbeat();
|
|
64
68
|
this.startTimeoutCheck();
|
|
@@ -140,12 +144,19 @@ class DouYinDanmaClient extends TypedEmitter {
|
|
|
140
144
|
}
|
|
141
145
|
}
|
|
142
146
|
reconnect() {
|
|
147
|
+
if (this.isReconnecting) {
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
143
150
|
this.stopHeartbeat();
|
|
144
151
|
this.stopTimeoutCheck();
|
|
145
152
|
if (this.reconnectAttempts < this.autoReconnect) {
|
|
153
|
+
this.isReconnecting = true;
|
|
146
154
|
this.reconnectAttempts++;
|
|
147
|
-
|
|
148
|
-
|
|
155
|
+
setTimeout(() => {
|
|
156
|
+
this.connect();
|
|
157
|
+
this.isReconnecting = false;
|
|
158
|
+
this.emit("reconnect", this.reconnectAttempts);
|
|
159
|
+
}, this.reconnectInterval);
|
|
149
160
|
}
|
|
150
161
|
}
|
|
151
162
|
async handleMessage() { }
|
package/package.json
CHANGED
package/types/types.d.ts
CHANGED
|
@@ -150,7 +150,9 @@ export interface SocialMessage {
|
|
|
150
150
|
export interface GiftMessage {
|
|
151
151
|
common: Common & { method: "WebcastGiftMessage" };
|
|
152
152
|
user: User;
|
|
153
|
+
// 这个参数其实差不多是秒级时间戳,并不可靠
|
|
153
154
|
groupId: string;
|
|
155
|
+
giftId: number;
|
|
154
156
|
gift: Gift;
|
|
155
157
|
publicAreaCommon: PublicAreaCommon;
|
|
156
158
|
trayDisplayText: TrayDisplayText;
|