Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

监听弹幕时获取用户头像 #27

Open
3 tasks done
sunft1996 opened this issue Jun 6, 2023 · 3 comments
Open
3 tasks done

监听弹幕时获取用户头像 #27

sunft1996 opened this issue Jun 6, 2023 · 3 comments
Labels
enhancement New feature or request

Comments

@sunft1996
Copy link

Clear and concise description of the problem

如题

Suggested solution

如题

Alternative

No response

Additional context

No response

Validations

@sunft1996 sunft1996 added the enhancement New feature or request label Jun 6, 2023
@ddiu8081
Copy link
Owner

目前原始弹幕消息中不包含头像数据,获取头像需要手动通过接口取,给框架添加此功能可能带来一些问题:

  1. 可能会引入额外依赖,目前仅读取 Websocket / TCP 流,如果读取 HTTP 接口可能还需要增加依赖库
  2. 弹幕密集时可能会被频控限制
  3. 浏览器环境存在跨域问题,无法获取

因此添加头像可能比较困难,目前建议是自行维护头像获取和频控逻辑。之后调研如果方便的话 会内置进来

@micooz
Copy link

micooz commented Jul 25, 2023

也许能用隔壁的方案:xfgryujk/blivedm@2b04284

具体做法是在 DANMU_MSG.parser 中添加解析 dm_v2 字段的代码。

const parser = (data: DataType, roomId: number): DanmuMsg => {
const rawData = data.info
const content = rawData[1]

dm_v2 是一个经过 base64 编码的 protobuf 报文,里面包含用户头像字段,协议格式在上面的 blivedm/pb_models.py 中有实现。

echo -n "CiIxMmM2ZDg4MGQ5Yjc3Njc3NTI1NTA1NjFjYjY0YTJmYjU5EAEYGSDP2v8HKghkODU4ZWQ3MzIG5ZGc5ZGcOMHUk+WRMUjl2cv+AWIAaAFydwoG5ZGc5ZGcEm0KE3Jvb21fMTA0MTMwNTFfMzY5NDkSSmh0dHBzOi8vaTAuaGRzbGIuY29tL2Jmcy9nYXJiLzMxYjI4ZjRlZjQ0NmYxNmYzZDEyZGU3ZTYzMmFlNjBhMmE0NDAyZWIucG5nGAEgASgBMKIBOKIBigEAmgEQCghFRTQ5MzU1OBCd9oulBqIBpQEIwofiBBIP5biD5LiB55Wq6IyE6IyEIkpodHRwczovL2kwLmhkc2xiLmNvbS9iZnMvZmFjZS9mY2NjY2MxOTQ3N2M0YzYzMGE0MjMwMjliYmViYjk1N2NkZGFkOWMyLmpwZziQTkABWiMIERIJ54ix6I2U5LidIKS6ngYwpLqeBjikup4GQKS6ngZQAWIPCBUQ3q3iAhoGPjUwMDAwagByAHoCCB+qARoIt+vxwQQSDeiNlOaenVl1cmliaXUY+8f7BA==" | base64 -d | hexdump -C

可能还需要引入一个类似 pure-protobuf 的 decoder 来方便解析。

@Tsuk1ko
Copy link

Tsuk1ko commented Sep 22, 2023

可以使用 protobufjs,分享一个我自己测试可行的方案

import { length as b64len, decode as b64dec } from '@protobufjs/base64';
import { Type, Field } from 'protobufjs';

const decodeB64 = str => {
  const length = b64len(str);
  const buffer = new Uint8Array(length);
  b64dec(str, buffer, 0);
  return buffer;
};

const UserInfo = new Type('UserInfo').add(new Field('face', 4, 'string'));
const DanmakuMessageV2 = new Type('DanmakuMessageV2').add(UserInfo).add(new Field('user', 20, 'UserInfo'));

/**
 * @param {string} str
 * @returns {{ user: { face: string } }}
 */
export const decodeDmV2 = str => {
  const buffer = decodeB64(str);
  return DanmakuMessageV2.decode(buffer);
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants