douyin-danma-listener 0.2.0 → 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 +17 -3
- package/package.json +1 -1
- package/types/types.d.ts +4 -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();
|
|
@@ -51,6 +54,7 @@ class DouYinDanmaClient extends TypedEmitter {
|
|
|
51
54
|
this.emit("error", new Error("获取抖音弹幕签名失败"));
|
|
52
55
|
return;
|
|
53
56
|
}
|
|
57
|
+
this.emit("init", url);
|
|
54
58
|
const cookies = this.cookie || (await getCookie());
|
|
55
59
|
this.ws = new WebSocket(url, {
|
|
56
60
|
headers: {
|
|
@@ -58,6 +62,7 @@ class DouYinDanmaClient extends TypedEmitter {
|
|
|
58
62
|
},
|
|
59
63
|
});
|
|
60
64
|
this.ws.on("open", () => {
|
|
65
|
+
this.reconnectAttempts = 0;
|
|
61
66
|
this.emit("open");
|
|
62
67
|
this.startHeartbeat();
|
|
63
68
|
this.startTimeoutCheck();
|
|
@@ -88,7 +93,9 @@ class DouYinDanmaClient extends TypedEmitter {
|
|
|
88
93
|
this.reconnectAttempts = this.autoReconnect;
|
|
89
94
|
this.stopHeartbeat();
|
|
90
95
|
this.stopTimeoutCheck();
|
|
91
|
-
this.ws.
|
|
96
|
+
if (this.ws.readyState === WebSocket.OPEN) {
|
|
97
|
+
this.ws.close();
|
|
98
|
+
}
|
|
92
99
|
}
|
|
93
100
|
startHeartbeat() {
|
|
94
101
|
if (this.isHeartbeatRunning) {
|
|
@@ -137,12 +144,19 @@ class DouYinDanmaClient extends TypedEmitter {
|
|
|
137
144
|
}
|
|
138
145
|
}
|
|
139
146
|
reconnect() {
|
|
147
|
+
if (this.isReconnecting) {
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
140
150
|
this.stopHeartbeat();
|
|
141
151
|
this.stopTimeoutCheck();
|
|
142
152
|
if (this.reconnectAttempts < this.autoReconnect) {
|
|
153
|
+
this.isReconnecting = true;
|
|
143
154
|
this.reconnectAttempts++;
|
|
144
|
-
|
|
145
|
-
|
|
155
|
+
setTimeout(() => {
|
|
156
|
+
this.connect();
|
|
157
|
+
this.isReconnecting = false;
|
|
158
|
+
this.emit("reconnect", this.reconnectAttempts);
|
|
159
|
+
}, this.reconnectInterval);
|
|
146
160
|
}
|
|
147
161
|
}
|
|
148
162
|
async handleMessage() { }
|
package/package.json
CHANGED
package/types/types.d.ts
CHANGED
|
@@ -150,13 +150,17 @@ 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;
|
|
157
159
|
totalCount: string;
|
|
158
160
|
clientGiftSource: number;
|
|
159
161
|
sendTime: string;
|
|
162
|
+
// repeatEnd 表示礼物连击完毕,这个为1时,sendTime值为秒级时间戳,否则为毫秒级时间戳
|
|
163
|
+
repeatEnd: null | 1;
|
|
160
164
|
}
|
|
161
165
|
|
|
162
166
|
export interface RoomUserSeqMessage {
|