npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

bilibili-ws-client

v3.0.0

Published

A client for the Bilibili Live Websocket.

Readme

bilibili-ws-client

适用于 Bilibili 直播的 WebSocket 客户端。

可用于实时获取弹幕 DANMU_MSG、收到礼物 SEND_GIFT、开播 LIVE、下播 PREPARING 等信息。

起步

bilibili-ws-client 支持以 CommonJSESM 的方式使用。

也支持从 HTML 文件和 script 标签开始。

通过以下方式安装

npm install bilibili-ws-client

在 node.js 使用

[!TIP]
buvid 通过 https://api.bilibili.com/x/frontend/finger/spi 获取
key 通过 https://api.live.bilibili.com/xlive/web-room/v1/index/getDanmuInfo?id=${roomId} 获取

作为 ESM 模块使用:

import Client from 'bilibili-ws-client';

作为 CommonJS 模块使用:

const Client = require('bilibili-ws-client');

const sub = new Client({
  uid: 0, // 留空或为 0 代表访客,无法显示弹幕发送用户
  roomid: 1,
  protover: 3,
  buvid: '', // b_3
  platform: 'web',
  type: 2,
  key: '', // token
});

sub.on('open', () => {
  // ...
  console.log('authorized');
});

sub.on('close', () => {
  // ...
});

sub.on('message', ({ ver, op, cmd, body, ts }) => {
  if (op === 3) {
    console.log('online: ' + body);
  } else if (op === 5) {
    switch (cmd) {
      case 'LIVE':
        // 开播
        // ...
        break;
      case 'PREPARING':
        // 闲置(下播)
        // ...
        break;
      case 'DANMU_MSG':
        console.log(body);
        break;
      default:
        break;
    }
  } else {
    // ...
    console.log('op: ' + op);
    console.log('body: ' + body);
  }
});

sub.on('error', (err) => {
  throw new Error(err);
});

在前端项目使用

作为 ESM 模块使用:

import Client from 'bilibili-ws-client';

const sub = new Client(roomId);
// ...

在网页中直接使用

将 js 添加到 HTML 中,且无需安装,即可立即开始使用。

添加 script 标签,应该如下所示:

<script src="https://unpkg.com/bilibili-ws-client/lib/index.umd.js"></script>
<script>
  const sub = new Client(1); // 需要鉴权

  sub.on('open', () => {
    // ...
    console.log('authorized');
  });

  sub.on('message', ({ ver, op, cmd, body, ts }) => {
    if (op === 3) {
      console.log('online: ' + body);
    } else if (op === 5) {
      switch (cmd) {
        case 'LIVE':
          // 开播
          // ...
          break;
        case 'PREPARING':
          // 闲置(下播)
          // ...
          break;
        case 'DANMU_MSG':
          console.log(body);
          break;
        default:
          break;
      }
    }
  });
</script>

配置

token

Type:

type Token =
  | number // roomId
  | Partial<{
      uid: number;
      roomid: number; // roomId
      protover: Ver; // 1 | 2 | 3
      buvid: string;
      platform: string; // 'web'
      clientver: string;
      type: number;
      key: string;
    }>;

需要连接的房间号或一个包含登录信息的对象。

enableLog

Type:

type enableLog = boolean | undefined;

默认值:false

是否记录日志,将通过 console.log 输出。

maxConnectTimes

Type:

type maxConnectTimes = number | undefined;

默认值:10

WebSocket 触发 close 时,最多重试再次连接的次数。

delay

Type:

type delay = number | undefined;

默认值:15000

WebSocket 触发 close 时,间隔特定毫秒后重试。

开发

npm run prettier
npm run build