bilibili-danmaku-client

2.0.0 • Public • Published

Header image

npm npm david-dm Maintainability

Bilibili Danmaku Client

A standalone client for connecting to the Bilibili Live Danmaku Interface, in Node.js and the browser.

Read this in another language: English, 简体中文.

Table of content

Introduction

Never heard of Bilibili or Danmaku? You're out of this. Close this page and have a cup of coffee might be the wise choice.

And if you're finding a way to watch on a Bilibili Live room, analyze danmaku programmatically, either for statistical or some squeaky purposes, you've come the right place. bilibili-danmaku-client will be exactly what you want.

bilibili-danmaku-client is a npm package that runs in Node.js or in the browser, providing an easy, fluent API for listening to the activities happening in a Bilibili Live room.

Installation

It can't be easier to install a npm package.

First make sure that you have npm and node correctly installed and in PATH, and then:

    $ npm install --save bilibili-danmaku-client

If you see something like peer dependencies not installed, don't panic, see this sector below.

Usage

If you wonder what you can do with this package, please see this demo first.

bilibili-danmaku-client is easy to use. You can:

Open a connection

    const DanmakuClient = require('bilibili-danmaku-client');
    // https://live.bilibili.com/5440
    const client = new DanmakuClient(5440);
    client.start();

Listen to Events

    const client = ...;
    
    const onDanmaku = ({ content, sender }) =>
        console.log(`${sender.name}${content}`);
    const onGift = ({ gift, num, sender }) =>
        console.log(`${sender.name} => ${gift.name} * ${num}`);
 
    client.on('event', ({ name, content }) => {
        switch (name) {
        case 'danmaku':
            onDanmaku(content);
            break;
        case 'gift':
            onGift(content);
            break;
        }
    })

For more information about Events, see Wiki.

Listen to client lifecycle

    const client = ...;
    client.on('open', () => console.log('Client opened.'));
    client.on('close', () => console.log('Client closed.'));

Terminate client

    const client = ...;
    client.terminate();
    client.on('close' () => console.log('Client closed.'));

Note that you must listen to the 'close' event to be notified when the client is actually closed. terminate() only requests close, not forces it.

For more information about how to use DanmakuClient, see Wiki.

Usage in browser

Actually this is not about bilibili-danmaku-client itself, but about the x-platform-ws package, which bilibili-danmaku-client depends on, and is a small utility package of mine.

In order to be compatible both in Node.js and in the browser, x-platform-ws requies shimming of the buffer and events Node.js module, therefore it declares them as peer dependencies. So if you run npm install, you might hear it complain about not installed peer dependecies.

Don't panic. That's perfectly unproblematic if you're only using bilibili-danmaku-client in Node.js, as shimming is not required there. But if you tend to use the package in a browser, you should read on.

I recommend using a bundler like browserify or webpack. they will automatically detect usage of buffer and events and provide shimming. Unfortunately, I happen to have no knowledge about how to use this package without a bundler, so - you have to figure the solution out yourself.

Development

To build and test the package yourself:

  1. Clone the github repository
    $ git clone -b master https://github.com/std4453/bilibili-danmaku-client.git
  1. Build
    $ npm install && npm run build
  1. Test
    $ npm test

Links

Author

License

This project is licensed under the MIT License, see LICENSE for details.

Readme

Keywords

Package Sidebar

Install

npm i bilibili-danmaku-client

Weekly Downloads

2

Version

2.0.0

License

MIT

Unpacked Size

381 kB

Total Files

45

Last publish

Collaborators

  • std4453