@floating-live/bilibili 0.16.16 → 0.17.0-alpha.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.
package/LICENSE CHANGED
@@ -18,4 +18,4 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
18
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
19
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
20
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
21
+ SOFTWARE.
@@ -549,6 +549,7 @@ export function parseGetInfoByRoom(data) {
549
549
  openStatus: 0,
550
550
  opened: false,
551
551
  connectionStatus: 0,
552
+ valid: true,
552
553
  };
553
554
  }
554
555
  /** 转换bilibili直播间信息为floating通用格式 */
@@ -580,6 +581,7 @@ export function parseRoomBaseInfo(data) {
580
581
  openStatus: 0,
581
582
  opened: false,
582
583
  connectionStatus: 0,
584
+ valid: true,
583
585
  };
584
586
  }
585
587
  export function mergeLiveRoomData(...list) {
@@ -25,11 +25,12 @@ export class PluginBilibili extends BasePlugin {
25
25
  this.room = null;
26
26
  }
27
27
  init(ctx) {
28
- ctx.whenRegister("platform", (platform) => {
28
+ ctx.whenRegister("platform", () => {
29
+ const platform = ctx.getPluginExposes("platform");
29
30
  platform.register("bilibili", platformInfo, ctx.signal);
30
31
  });
31
- ctx.whenRegister("room", (room) => {
32
- this.room = room;
32
+ ctx.whenRegister("room", () => {
33
+ this.room = ctx.getPluginExposes("room");
33
34
  return () => {
34
35
  this.room = null;
35
36
  };
@@ -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;
@@ -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", () => {
package/package.json CHANGED
@@ -1,37 +1,45 @@
1
1
  {
2
2
  "name": "@floating-live/bilibili",
3
- "version": "0.16.16",
3
+ "version": "0.17.0-alpha.0",
4
4
  "type": "module",
5
- "main": "index.js",
6
- "types": "index.d.ts",
5
+ "main": "./dist/index.js",
6
+ "types": "./dist/index.d.ts",
7
7
  "author": "Minteea",
8
8
  "license": "MIT",
9
9
  "scripts": {
10
10
  "check": "tsc --noEmit",
11
- "clear": "rimraf **/*.js **/*.d.ts --glob",
11
+ "clear": "rimraf ./dist",
12
12
  "build": "npm run clear && npm run check && tsc --build && tsc-alias",
13
13
  "prepack": "npm run build",
14
- "postpack": "npm run clear"
14
+ "postpack": "npm run clear",
15
+ "build:plugin": "vite build --config plugin.vite.config.ts",
16
+ "version": "floating-live-plugin-tools version && git add plugin.json"
15
17
  },
16
18
  "publishConfig": {
17
19
  "access": "public"
18
20
  },
19
21
  "dependencies": {
20
22
  "@floating-live/cookies": "^0.1.0",
21
- "bilibili-live-danmaku": "^0.7.13",
23
+ "bilibili-live-danmaku": "^0.7.15",
22
24
  "md5": "^2.3.0"
23
25
  },
24
26
  "devDependencies": {
27
+ "@floating-live/plugin-tools": "^0.1.0-alpha.5",
25
28
  "@types/md5": "^2.3.0",
26
29
  "@types/websocket": "^1.0.5",
27
30
  "@types/ws": "^8.5.3",
28
- "floating-live": "^0.16.6",
31
+ "floating-live": "^0.17.0-alpha.1",
29
32
  "rimraf": "^3.0.2",
30
33
  "tsc-alias": "^1.8.16",
31
- "typescript": "^5.9.3"
34
+ "typescript": "^5.9.3",
35
+ "vite": "^8.0.11"
32
36
  },
33
37
  "peerDependencies": {
34
- "floating-live": "^0.16.6"
38
+ "floating-live": "0.17.0-alpha.1"
35
39
  },
36
- "gitHead": "67c413a99f964efabfec55c90fc5803c653a6c62"
40
+ "files": [
41
+ "dist",
42
+ "plugin.json"
43
+ ],
44
+ "gitHead": "eb7c02f5dba6b6abc223b747cdf391942ae9e734"
37
45
  }
package/plugin.json ADDED
@@ -0,0 +1,7 @@
1
+ {
2
+ "name": "bilibili",
3
+ "author": "FloatingLive",
4
+ "version": "0.17.0-alpha.0",
5
+ "description": "Bilibili plugin for FloatingLive",
6
+ "main": "plugin.js"
7
+ }
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes