Git Product home page Git Product logo

tiny-bilibili-ws's Introduction

tiny-bilibili-ws

Version Build Size

特点

  • 同时支持浏览器和NodeJs环境
  • 开箱即用的 Typescript 类型提示
  • 轻量级的包体积

文档

阅读 文档 查看更多信息.

安装

Npm

npm install --save tiny-bilibili-ws

Yarn

yarn add tiny-bilibili-ws

Pnpm

pnpm install tiny-bilibili-ws

简单使用

// In Nodejs environment
import { KeepLiveTCP, toMessageData, getLongRoomId } from 'tiny-bilibili-ws'

const { data } = await getLongRoomId(652581)
const room = data.room_id
const tcp = new KeepLiveTCP(room)

tcp.runWhenConnected(() => {
  console.log(`正在监听 ${room}`)
})

tcp.on('heartbeat', (online) => {
  console.log('人气值: ', online)
})

tcp.on('DANMU_MSG', (danmu) => {
  console.log(toMessageData(danmu))
})

tcp.on('error', (e) => {
  console.error('错误: ', e)
})

tcp.on('close', () => {
  console.log(`退出监听 ${room}`)
})

// or browser environment
import { KeepLiveWS } from 'tiny-bilibili-ws/browser'

// 因为存在跨域问题, getLongRoomId 这个 API 不能在浏览器中运行

new KeepLiveWS(652581) // Now Long Room id: 4350043

Credits

Inspired by https://github.com/simon300000/bilibili-live-ws

参考资料

Bilibili API: https://github.com/lovelyyoshino/Bilibili-Live-API/blob/master/API.WebSocket.md

License

MIT License ©2022 starknt

tiny-bilibili-ws's People

Contributors

ddiu8081 avatar starknt avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

tiny-bilibili-ws's Issues

关于cookie的使用方式?

没了解过 websocket 如何分辨登入方式,但假设我手上有 b 站账户的 cookie, 我可以怎么传入到这个库里面(又或者怎么让它成为登入状态)?

是否无法支持短房号?

发现有些房间的房间号是比较短的,房号只有 3、4 位(好像是排行榜靠前的或者一些比较火的主播会有这样的房间号),通过这种短的房号无法抓到弹幕;不知道是什么原因呢?有点疑惑;有没有一些解决方法?

在浏览器环境下要调用reconnect()函数才能连接这是正常的么?

import { KeepLiveWS, toMessageData } from 'tiny-bilibili-ws/browser'
const room = 21507503
const ws = new KeepLiveWS(room);
ws.reconnect(); // 需要调用这个函数才能开始监听
ws.runWhenConnected(() => {
  console.log(`正在监听 ${room}`)
})

ws.on('DANMU_MSG', (danmu) => {
  console.log(toMessageData(danmu))
})

ws.on('error', (e) => {
  console.error('错误: ', e)
})

弹幕消息 cmd 更改为 DANMU_MSG:4:0:2:2:2:0 适配

或许是近日临时举措,b 站关闭了漂浮弹幕,使用原来的 DANMU_MSG 监听不到消息了,改为 DANMU_MSG:4:0:2:2:2:0

研究了下 bilibili-live-ws 做了一些适配,会把以 DANMU_MSG 开头的消息都当作 DANMU_MSG,从而能够继续监听到

if (cmd) {
  if (cmd.includes('DANMU_MSG')) {
    this.emit('DANMU_MSG', data)
  } else {
    this.emit(cmd, data)
  }
}

这个库是否也可以做类似的处理?

相关 issue: ddiu8081/blive-message-listener#14

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.