A standalone client for connecting to the Bilibili Live Danmaku Interface, in Node.js and the browser.
Read this in another language: English, 简体中文.
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.
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.
If you wonder what you can do with this package, please see this demo first.
bilibili-danmaku-client
is easy to use. You can:
const DanmakuClient = require('bilibili-danmaku-client');
// https://live.bilibili.com/5440
const client = new DanmakuClient(5440);
client.start();
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.
const client = ...;
client.on('open', () => console.log('Client opened.'));
client.on('close', () => console.log('Client closed.'));
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.
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.
To build and test the package yourself:
- Clone the github repository
$ git clone -b master https://github.com/std4453/bilibili-danmaku-client.git
- Build
$ npm install && npm run build
- Test
$ npm test
- Live demo
- npm package
- Zhihu article (Chinese)
- API reference
- std4453 - me@std4453.com - blog
This project is licensed under the MIT License, see LICENSE for details.