{"id":13431555,"url":"https://github.com/varieget/bilibili-ws-client","last_synced_at":"2025-03-16T11:31:56.045Z","repository":{"id":42764123,"uuid":"466110993","full_name":"varieget/bilibili-ws-client","owner":"varieget","description":"适用于b站直播的 WebSocket 客户端","archived":false,"fork":false,"pushed_at":"2025-02-03T07:35:49.000Z","size":633,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-04T07:49:20.484Z","etag":null,"topics":["bilibili","bilibili-live","websocket"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/varieget.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2022-03-04T12:16:41.000Z","updated_at":"2025-02-01T22:57:40.000Z","dependencies_parsed_at":"2024-02-26T19:47:41.539Z","dependency_job_id":"b84cc94b-c83a-4edf-8260-64cf793261d3","html_url":"https://github.com/varieget/bilibili-ws-client","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/varieget%2Fbilibili-ws-client","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/varieget%2Fbilibili-ws-client/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/varieget%2Fbilibili-ws-client/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/varieget%2Fbilibili-ws-client/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/varieget","download_url":"https://codeload.github.com/varieget/bilibili-ws-client/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243863283,"owners_count":20360290,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["bilibili","bilibili-live","websocket"],"created_at":"2024-07-31T02:01:04.036Z","updated_at":"2025-03-16T11:31:56.031Z","avatar_url":"https://github.com/varieget.png","language":"TypeScript","funding_links":[],"categories":["开发"],"sub_categories":["直播脚本"],"readme":"# bilibili-ws-client\n\n适用于 Bilibili 直播的 `WebSocket` 客户端。\n\n可用于实时获取弹幕 `DANMU_MSG`、收到礼物 `SEND_GIFT`、开播 `LIVE`、下播 `PREPARING` 等信息。\n\n## 起步\n\n`bilibili-ws-client` 支持以 `CommonJS` 或 `ESM` 的方式使用。\n\n也支持从 HTML 文件和 script 标签开始。\n\n### 通过以下方式安装\n\n```bash\nnpm install bilibili-ws-client\n```\n\n### 在 node.js 使用\n\n\u003e [!TIP]  \n\u003e `buvid` 通过 https://api.bilibili.com/x/frontend/finger/spi 获取  \n\u003e `key` 通过 https://api.live.bilibili.com/xlive/web-room/v1/index/getDanmuInfo?id=${roomId} 获取\n\n作为 `ESM` 模块使用：\n\n```js\nimport Client from 'bilibili-ws-client';\n```\n\n作为 `CommonJS` 模块使用：\n\n```js\nconst Client = require('bilibili-ws-client');\n\nconst sub = new Client({\n  uid: 0, // 留空或为 0 代表访客，无法显示弹幕发送用户\n  roomid: 1,\n  protover: 3,\n  buvid: '', // b_3\n  platform: 'web',\n  type: 2,\n  key: '', // token\n});\n\nsub.on('open', () =\u003e {\n  // ...\n  console.log('authorized');\n});\n\nsub.on('close', () =\u003e {\n  // ...\n});\n\nsub.on('message', ({ ver, op, cmd, body, ts }) =\u003e {\n  if (op === 3) {\n    console.log('online: ' + body);\n  } else if (op === 5) {\n    switch (cmd) {\n      case 'LIVE':\n        // 开播\n        // ...\n        break;\n      case 'PREPARING':\n        // 闲置（下播）\n        // ...\n        break;\n      case 'DANMU_MSG':\n        console.log(body);\n        break;\n      default:\n        break;\n    }\n  } else {\n    // ...\n    console.log('op: ' + op);\n    console.log('body: ' + body);\n  }\n});\n\nsub.on('error', (err) =\u003e {\n  throw new Error(err);\n});\n```\n\n### 在前端项目使用\n\n作为 `ESM` 模块使用：\n\n```js\nimport Client from 'bilibili-ws-client';\n\nconst sub = new Client(roomId);\n// ...\n```\n\n### 在网页中直接使用\n\n将 js 添加到 HTML 中，且无需安装，即可立即开始使用。\n\n添加 script 标签，应该如下所示：\n\n```html\n\u003cscript src=\"https://unpkg.com/bilibili-ws-client/lib/index.umd.js\"\u003e\u003c/script\u003e\n\u003cscript\u003e\n  const sub = new Client(1); // 需要鉴权\n\n  sub.on('open', () =\u003e {\n    // ...\n    console.log('authorized');\n  });\n\n  sub.on('message', ({ ver, op, cmd, body, ts }) =\u003e {\n    if (op === 3) {\n      console.log('online: ' + body);\n    } else if (op === 5) {\n      switch (cmd) {\n        case 'LIVE':\n          // 开播\n          // ...\n          break;\n        case 'PREPARING':\n          // 闲置（下播）\n          // ...\n          break;\n        case 'DANMU_MSG':\n          console.log(body);\n          break;\n        default:\n          break;\n      }\n    }\n  });\n\u003c/script\u003e\n```\n\n## 配置\n\n- **[`token`](#token)**\n- **[`enableLog`](#enableLog)**\n- **[`maxConnectTimes`](#maxConnectTimes)**\n- **[`delay`](#delay)**\n\n### `token`\n\nType:\n\n```ts\ntype Token =\n  | number // roomId\n  | Partial\u003c{\n      uid: number;\n      roomid: number; // roomId\n      protover: Ver; // 1 | 2 | 3\n      buvid: string;\n      platform: string; // 'web'\n      clientver: string;\n      type: number;\n      key: string;\n    }\u003e;\n```\n\n需要连接的房间号或一个包含登录信息的对象。\n\n### `enableLog`\n\nType:\n\n```ts\ntype enableLog = boolean | undefined;\n```\n\n默认值：`false`\n\n是否记录日志，将通过 `console.log` 输出。\n\n### `maxConnectTimes`\n\nType:\n\n```ts\ntype maxConnectTimes = number | undefined;\n```\n\n默认值：`10`\n\n当 `WebSocket` 触发 `close` 时，最多重试再次连接的次数。\n\n### `delay`\n\nType:\n\n```ts\ntype delay = number | undefined;\n```\n\n默认值：`15000`\n\n当 `WebSocket` 触发 `close` 时，间隔特定毫秒后重试。\n\n## 开发\n\n```bash\nnpm run prettier\nnpm run build\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvarieget%2Fbilibili-ws-client","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fvarieget%2Fbilibili-ws-client","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvarieget%2Fbilibili-ws-client/lists"}