client/
models.rs

1// src/client/models.rs
2//! Data models for Bilibili live danmaku WebSocket client
3
4use serde::{Deserialize, Serialize};
5use std::collections::HashMap;
6
7#[derive(Debug)]
8pub struct DanmuServer {
9    pub host: String,
10    pub port: i32,
11    pub wss_port: i32,
12    pub ws_port: i32,
13}
14
15impl Default for DanmuServer {
16    fn default() -> Self {
17        Self {
18            host: String::from("broadcastlv.chat.bilibili.com"),
19            port: 2243,
20            wss_port: 443,
21            ws_port: 2244,
22        }
23    }
24}
25
26#[derive(Copy, Clone, Debug)]
27pub struct MsgHead {
28    pub pack_len: u32,
29    pub raw_header_size: u16,
30    pub ver: u16,
31    pub operation: u32,
32    pub seq_id: u32,
33}
34
35#[derive(Debug, Serialize, Deserialize)]
36pub struct AuthMessage {
37    pub uid: u64,
38    pub roomid: u64,
39    pub protover: i32,
40    pub platform: String,
41    pub type_: i32,
42    pub key: String,
43}
44
45impl AuthMessage {
46    pub fn from(map: &HashMap<String, String>) -> AuthMessage {
47        AuthMessage {
48            uid: map.get("uid").unwrap().parse::<u64>().unwrap(),
49            roomid: map.get("room_id").unwrap().parse::<u64>().unwrap(),
50            protover: 3,
51            platform: "web".to_string(),
52            type_: 2,
53            key: map.get("token").unwrap().to_string(),
54        }
55    }
56}
57
58#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
59pub enum BiliMessage {
60    Danmu { user: String, text: String },
61    Gift { user: String, gift: String },
62    // Add more variants as needed
63    Unsupported,
64}
65
66#[cfg(test)]
67mod tests {
68    use super::*;
69
70    #[test]
71    fn test_auth_message_from_map() {
72        let mut map = std::collections::HashMap::new();
73        map.insert("uid".to_string(), "12345".to_string());
74        map.insert("room_id".to_string(), "67890".to_string());
75        map.insert("token".to_string(), "test_token".to_string());
76        let auth = AuthMessage::from(&map);
77        assert_eq!(auth.uid, 12345);
78        assert_eq!(auth.roomid, 67890);
79        assert_eq!(auth.key, "test_token");
80    }
81}