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 🙏

© 2024 – Pkg Stats / Ryan Hefner

bili-live-listener

v1.0.5

Published

Bilibili直播间消息监听库。

Downloads

88

Readme

bili-live-listener

NPM Version Bilibili

Bilibili直播间消息监听库。

  • 更优雅的消息监听方式
  • 更丰富的类型导出
  • 持续更新的API
  • 基于 TCP 协议,~~也许更节省内存~~

Install

npm i bili-live-listener

Example

import { BiliLive } from 'bili-live-listener'

// 获取直播间长号
const { longRoomId } = getRoomId(510)

// 实例化一个 BiliLive 对象,传入直播间号
// 需要传入登录状态下获取的 key 以及 uid,详情见下方 Options
const biliLive = new BiliLive(longRoomId, {
  key: 'xxxxxxxx', // your key
  uid: 12345678, // your logined uid
  // isBrowser: true // if you are in browser environment
})

// 监听弹幕消息,返回一个移除监听器的函数
const removeHandler = biliLive.onDanmu(({ data }) => {
  console.log(`${data.user.uname} : ${data.content}`)
})

// 移除监听器
removeHandler()

// 切断连接
biliLive.close()

Class Params

[!IMPORTANT] ~~叔叔~~B站修改了获取直播间数据包的要求,现在必须传入登录状态下获取的 key 以及 登录的 uid 才能正常接受到数据包。

BiliLive 的构造函数接受两个参数,分别为 roomIdoptions

roomId

要监听的直播间号,需要传入长号。

node 环境下,可以使用本库提供的 getRoomId 函数获取长号与短号(如果有)。

import { getRoomId } from 'bili-live-listener'

console.log(getRoomId(510))
// => { longRoomId: 80397, shortRoomId: 510 }

console.log(getRoomId(80397))
// => { longRoomId: 80397, shortRoomId: 510 }

options

key

登录状态下获取的 key

登录状态下,在浏览器中访问链接 https://api.live.bilibili.com/room/v1/Room/mobileRoomInit?id={LONG_ROOM_ID} , 找到响应体中的 data.token 即为 key

node 环境下,可以使用本库提供的 getRoomConf 函数获取,示例如下:

import { getRoomConf } from 'bili-live-listener'

// bilibili.com 中的 cookie,注意不要通过 `document.cookie` 获取,而应该通过浏览器发送请求的请求头中的 `cookie` 属性获取
const cookie = 'buvid3=...'

// 注意传入长号
const longRoomId = 80397

const { key } = getRoomConf(longRoomId, cookie)

uid

登录的 uid

登录状态下,在浏览器中访问 https://api.bilibili.com/x/web-interface/nav ,找到响应体中的 data.mid 即为 uid

当然,你也可以直接访问自己的主页,找到 uid

node 环境下,可以使用本库提供的 getLoginedUid 函数获取,示例如下:

import { getLoginedUid } from 'bili-live-listener'

// bilibili.com 中的 cookie,注意不要通过 `document.cookie` 获取,而应该通过浏览器发送请求的请求头中的 `cookie` 属性获取
const cookie = 'buvid3=...'

const uid = getLoginedUid(cookie)

isBrowser

是否在浏览器环境下运行,默认为 false

浏览器环境下必须传入 true

Instance Methods

Common Methods

| Instance Method | Callback Params | Trigger When ... | | --------------- | -------------------- | ---------------------------- | | onOpen | void | 连接开启 | | onLive | void | 成功登入房间 | | onHeartbeat | void | 收到服务器心跳包,约30s一次 | | onClose | void | 连接关闭 | | onError | error: any | 连接 error,同时连接也会关闭 |

Listen to Raw Message

你可以使用 onRawMessage 方法监听原生消息,以应对B站新增的消息类型。一个例子如下:

// 监听一切消息
biliLive.onRawMessage('msg',(message) => {
  console.log(message)
})

// 监听指定消息
biliLive.onRawMessage('DANMU_MSG',(message) => {
  console.log(message)
})

Message Methods

| Instance Method | Callback Params | Trigger When ... | | --------------- | -------------------- | ---------------------------- | | onDanmu | DanmuData | 收到弹幕消息 | | onGuardBuy | GuardBuyData | 收到上船消息 | | onSuperChat | SuperChatData | 收到醒目留言消息 | | onGift | GiftData | 收到礼物消息 | | onWatchedChange | WatchedChangeData | “XX人看过”人数变动 | | onRankCountChange | RankCountChangeData | 高能榜人数变动 | | onLikeCountChange | LikeCountChangeData | 点赞数变动 | | onNotice | NoticeData | 收到全站广播 | | onHotRankUpdate | HotRankUpdateData | 当前直播间的分区排名变动 | | onFansCountUpdate | FansCountUpdateData | 粉丝数量\粉丝团数量变动 | | onLiveStart | LiveStartData | 直播开启 | | onLiveEnd | LiveEndData | 直播结束 | | onInteract | InteractData | 互动消息(用户进入、关注、分享、点赞直播间) | | onEntryEffect | EntryEffectData | 有入场特效的用户进入直播间(舰长、提督、总督等) | | onRoomChange | RoomChangeData | 房间信息变动(标题、分区) | | onAnchorLotStart | AnchorLotStartData | 天选时刻开始 | | onAnchorLotEnd | AnchorLotEndData | 天选时刻结束 | | onRedPocketStart | RedPocketStartData | 红包抽奖开始 | | onRedPocketEnd | RedPocketEndData | 红包抽奖结束 | | onPopularRankUpdate | PopularRankUpdateData | 当前直播间的热门排名变动 | | onDanmuInteract | DanmuInteractData | 弹幕互动(弹幕投票信息、“他们都在说”类的弹幕连击) |

值得注意的是,这些 Message Methods 都会返回一个移除监听器的函数,你可以调用这个函数来移除监听器,参考 这里

Message Type

Common

// 回调函数中的参数类型
export interface Message<T> {
  /** 消息原生类型 */
  cmd: string
  /** 收到消息的时间戳,由 Date.now() 生成 */
  timestamp: number
  /** 类型化后的消息主体 */
  data: T
  /** 原生消息 */
  raw: any
}

[!TIP] 大部分消息主体 data 中都会包含 timestamp 字段,表示消息到达的更精确的秒时间戳,建议使用这个字段。 而 Message<T> 中的 timestamp 字段,是由 Date.now() 生成的时间戳,也许会有一定的误差。

Data Type

你可以在 src/events 查看所有的消息主体类型,它将作为泛型传入回调函数的参数类型 Message<T> 中。其中部分类型在 src/types/message.ts 中定义。

比如,弹幕消息的消息主体类型为 DanmuData,定义如下:

export interface DanmuData {
  /** 用户信息 */
  user: User
  /** 弹幕内容 */
  content: string
  /** 时间戳 */
  timestamp: number
  /** 是否为抽奖弹幕 */
  isLottery: boolean
  // 弹幕表情
  emoticon?: {
    id: number
    url: string
  }
}

Reference

SocialSisterYi/bilibili-API-collect

lovelyyoshino/Bilibili-Live-API