GithubHelp home page GithubHelp logo

shiqimei / bilive-danmaku Goto Github PK

View Code? Open in Web Editor NEW

This project forked from 4t145/bilive-danmaku

0.0 0.0 0.0 67 KB

bili直播api,对bilibili直播间的wss连接提供rust的基本支持(弹幕,礼物,sc之类),基于tokio

Rust 100.00%

bilive-danmaku's Introduction

bilive-danmaku

这个库提供模拟bilibili直播的wss连接的功能,目前还在开发中

上一个发布的版本:https://github.com/4t145/bilive-danmaku/tree/ver-0.1.0

使用

通过websocket

通过使用 https://github.com/4t145/rudanmaku-core

这使你可以通过ws来获取事件,计划在未来支持ipc通讯(uds for linux,命名管道 for windows)

作为库使用

因为使用了尚未稳定的split_array,因此需要切换到nightly版本

rustup override set nightly

Cargo.toml中加入

bilive-danmaku = { git = "https://github.com/4t145/bilive-danmaku", branch = "master" }

使用

use bilive_danmaku::{RoomService};


async fn service() {
    let service = RoomService::new(477317922).init().await.unwrap();
    let service = service.connect().await.unwrap();
    // 这里会获得一个 broadcast::Reciever<Event>
    let mut events_rx = service.subscribe();
    while let Some(evt) = events_rx.recv().await {
        // 处理事件
        todo!()
    }
    let service = service.close();
}

数据类型在model模块中, 事件类型在event模块中

use model::{User, FansMedal};
use event::Event as BiliEvent;

已经支持的事件

#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(tag = "tag", content="data")]
pub enum Event {
    Danmaku {
        // junk_flag=0 才是正常弹幕(非抽奖/天选等)
        // 抽奖,天选,junk_flag = 2
        // 其他值不知道有什么意义,所以暂且保留这个字段为u64
        junk_flag: u64,
        message: DanmakuMessage,
        user: User,
        fans_medal: Option<FansMedal>
    },
    EnterRoom {
        user: User,
        fans_medal: Option<FansMedal>
    },
    Gift {
        user: User,
        fans_medal: Option<FansMedal>,
        gift: Gift,
    },
    GuardBuy {
        level: u64,
        price: u64,
        user: User
    },
    SuperChat {
        user: User,
        fans_medal: Option<FansMedal>,
        price: u64, 
        message: String,
        message_jpn: Option<String>
    },
    WatchedUpdate {
        num: u64
    },
    PopularityUpdate {
        popularity: u32,
    },
    GuardEnterRoom {
        user: User,
    },
    HotRankChanged {
        area: String,
        rank: u64,
        description: String,
    },
    HotRankSettlement {
        uname: String,
        face: String,
        area: String,
        rank: u64,
    },
}

可参考:

feature flag

flag 功能
connect 连接直播间,默认启用
event 只启用model和event,不包含连接
bincode 启用bincode正反序列化
json 启用json正反序列化
verbose debug用,输出每条解析的json
debug debug用,输出解析错误

默认只启用connect 比如你想把收到的消息序列化为json格式,启用

[dependencies.bilive-danmaku]
# ****
features = ["connect", "json"]

JavaScript/TypeScript 支持

npm install [email protected]

使用例

import {Event, DanmakuEvent} from 'bilive-danmaku-json';
function on_danmaku(data: DanmakuEvent['data']) {
    if(data.junk_flag===0) {
        console.log(data.message);
    }
    // ...
}
// ... 获取data
const evt = JSON.parse(data);
if(evt.tag === 'Danmaku') {
    on_danmaku(evt.data);
}

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.