@floating-live/bilibili 0.16.16 → 0.16.17

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 (3) hide show
  1. package/package.json +3 -3
  2. package/room.d.ts +4 -0
  3. package/room.js +35 -13
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@floating-live/bilibili",
3
- "version": "0.16.16",
3
+ "version": "0.16.17",
4
4
  "type": "module",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
@@ -18,7 +18,7 @@
18
18
  },
19
19
  "dependencies": {
20
20
  "@floating-live/cookies": "^0.1.0",
21
- "bilibili-live-danmaku": "^0.7.13",
21
+ "bilibili-live-danmaku": "^0.7.15",
22
22
  "md5": "^2.3.0"
23
23
  },
24
24
  "devDependencies": {
@@ -33,5 +33,5 @@
33
33
  "peerDependencies": {
34
34
  "floating-live": "^0.16.6"
35
35
  },
36
- "gitHead": "67c413a99f964efabfec55c90fc5803c653a6c62"
36
+ "gitHead": "a97aa84ee559b632c278f4cc14bf1958c1b1eb97"
37
37
  }
package/room.d.ts CHANGED
@@ -59,6 +59,8 @@ export declare class RoomBilibili extends LiveRoom implements BilibiliRoomData {
59
59
  autoReconnect: boolean;
60
60
  /** 是否初始化 */
61
61
  whenInit: Promise<RoomBilibili>;
62
+ /** 重连次数 */
63
+ reconnectCount: number;
62
64
  get userAgent(): string;
63
65
  set userAgent(ua: string);
64
66
  /** 是否处于重连状态 */
@@ -94,6 +96,8 @@ export declare class RoomBilibili extends LiveRoom implements BilibiliRoomData {
94
96
  setTokens(tokens: ConnectTokens): void;
95
97
  /** 无缝重连 */
96
98
  reconnect(): void;
99
+ /** 间隔安全重连,等待一定连接间隔后可重连 */
100
+ safeReconnect(): void;
97
101
  private emitConnention;
98
102
  /** 打开直播弹幕客户端 */
99
103
  private openWebsocketClient;
package/room.js CHANGED
@@ -53,6 +53,8 @@ export class RoomBilibili extends LiveRoom {
53
53
  this.anchor = { name: "", id: 0 };
54
54
  /** 直播间弹幕api客户端 */
55
55
  this.client = null;
56
+ /** 重连次数 */
57
+ this.reconnectCount = 0;
56
58
  /** 连接凭证 */
57
59
  _RoomBilibili_tokens.set(this, void 0);
58
60
  /** 用户cookie */
@@ -198,12 +200,41 @@ export class RoomBilibili extends LiveRoom {
198
200
  this.reconnecting = true;
199
201
  const lastClient = this.client;
200
202
  this.initClient();
203
+ console.log("reconnect - " + Date.now());
204
+ // 控制连接事件成功或失败
205
+ const connectionAbortController = new AbortController();
201
206
  // 重连成功后,关闭上一个连接
202
207
  this.client?.addEventListener("open", () => {
208
+ connectionAbortController.abort();
203
209
  lastClient?.[symbolAbortController].abort();
204
- lastClient?.close();
210
+ lastClient && lastClient.ws.readyState < 2 && lastClient?.close();
205
211
  this.reconnecting = false;
206
- }, { once: true });
212
+ this.reconnectCount = 0;
213
+ }, { once: true, signal: connectionAbortController.signal });
214
+ // 若重连失败,关闭该连接、恢复上一个连接并重连
215
+ this.client?.addEventListener("error", () => {
216
+ connectionAbortController.abort();
217
+ const failedClient = this.client;
218
+ this.client = lastClient;
219
+ failedClient?.[symbolAbortController].abort();
220
+ failedClient && failedClient.ws.readyState < 2 && failedClient?.close();
221
+ this.reconnecting = false;
222
+ this.reconnectCount += 1;
223
+ this.safeReconnect();
224
+ }, { once: true, signal: connectionAbortController.signal });
225
+ }
226
+ /** 间隔安全重连,等待一定连接间隔后可重连 */
227
+ safeReconnect() {
228
+ const timeout = this.lastConnectionTime + this.connectInterval - Date.now();
229
+ if (timeout > 0) {
230
+ const t = setTimeout(() => {
231
+ this.reconnect();
232
+ clearTimeout(t);
233
+ }, timeout);
234
+ }
235
+ else {
236
+ this.reconnect();
237
+ }
207
238
  }
208
239
  emitConnention(status) {
209
240
  this.connectionStatus = status;
@@ -255,17 +286,8 @@ export class RoomBilibili extends LiveRoom {
255
286
  this.opened && this.emitConnention(LiveConnectionStatus.disconnected);
256
287
  // 自动重连
257
288
  if (this.opened && this.autoReconnect) {
258
- const timeout = this.lastConnectionTime + this.connectInterval - Date.now();
259
- this.lastConnectionTime = Date.now();
260
- if (timeout > 0) {
261
- const t = setTimeout(() => {
262
- this.reconnect();
263
- clearTimeout(t);
264
- }, timeout);
265
- }
266
- else {
267
- this.reconnect();
268
- }
289
+ this.reconnectCount += 1;
290
+ this.safeReconnect();
269
291
  }
270
292
  }, { signal });
271
293
  client.addEventListener("CONNECT_SUCCESS", () => {