Skip to content

Commit

Permalink
Merge pull request #141 from OrdinaryRoad-Project/fix-139-BilibiliDan…
Browse files Browse the repository at this point in the history
…muMsg

Fix 139 bilibili danmu msg
  • Loading branch information
1962247851 authored Jun 6, 2024
2 parents 54daa74 + 2c91ebc commit cc99f44
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public class BilibiliApis {
public static final TimedCache<Long, String> GIFT_IMG_CACHE = new TimedCache<>(TimeUnit.DAYS.toMillis(1));
public static final String KEY_COOKIE_CSRF = "bili_jct";
public static final String KEY_UID = "DedeUserID";
public static final String KEY_BUVID3 = "buvid3";
public static final String PATTERN_REAL_ROOM_ID = "\\\"roomInitRes\\\".+\\{\\\"room_id\\\":(\\d+)";

private static final String API_FRONTEND_FINGER_SPI = "https://api.bilibili.com/x/frontend/finger/spi";
Expand All @@ -72,13 +73,31 @@ public class BilibiliApis {
public static RoomInitResult roomInit(long roomId, String cookie) {
RoomPlayInfoResult roomPlayInfo = getRoomPlayInfo(roomId, cookie);
long realRoomId = roomPlayInfo.room_id;
// frontendFingerSpi();
String b_3 = OrLiveChatCookieUtil.getCookieByName(cookie, "buvid3", BilibiliApis::v);
// webInterfaceNav(cookie);

String b_3;
String uid;
if (StrUtil.isNotBlank(cookie)) {
uid = OrLiveChatCookieUtil.getCookieByName(cookie, KEY_UID, () -> {
throw new BaseException("cookie中缺少参数" + KEY_UID);
});
b_3 = OrLiveChatCookieUtil.getCookieByName(cookie, KEY_BUVID3, () -> {
throw new BaseException("cookie中缺少参数" + KEY_BUVID3);
});
// webInterfaceNav(cookie);
} else {
uid = "0";

JsonNode jsonNode = frontendFingerSpi();
b_3 = jsonNode.get("b_3").asText();
String b4 = jsonNode.get("b_4").asText();
cookie = KEY_BUVID3 + "=" + b_3
+ "; buvid4=" + b4;
}

DanmuinfoResult danmuInfo = getDanmuInfo(realRoomId, cookie);
return new RoomInitResult.RoomInitResultBuilder()
.buvid3(b_3)
.uid(OrLiveChatCookieUtil.getCookieByName(cookie, "DedeUserID", () -> "0"))
.uid(uid)
.danmuinfoResult(danmuInfo)
.roomPlayInfoResult(roomPlayInfo)
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
package tech.ordinaryroad.live.chat.client.bilibili.client;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.GlobalHeaders;
import cn.hutool.http.Header;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
Expand Down Expand Up @@ -122,18 +124,25 @@ protected void initChannel(SocketChannel channel) {

@Override
public BilibiliConnectionHandler initConnectionHandler(IBaseConnectionListener<BilibiliConnectionHandler> clientConnectionListener) {
DefaultHttpHeaders headers = new DefaultHttpHeaders();
headers.add(Header.USER_AGENT.name(), GlobalHeaders.INSTANCE.header(Header.USER_AGENT));
headers.add(Header.ORIGIN.name(), "https://live.bilibili.com");
headers.add(Header.PRAGMA.name(), "no-cache");
return new BilibiliConnectionHandler(
() -> new WebSocketClientProtocolHandler(
WebSocketClientProtocolConfig.newBuilder()
.webSocketUri(getWebsocketUri())
.version(WebSocketVersion.V13)
.subprotocol(null)
.allowExtensions(true)
.customHeaders(new DefaultHttpHeaders())
.maxFramePayloadLength(getConfig().getMaxFramePayloadLength())
.handshakeTimeoutMillis(getConfig().getHandshakeTimeoutMillis())
.build()
),
() -> {
headers.set(Header.HOST.name(), getWebsocketUri().getHost() + ":" + getWebsocketUri().getPort());
return new WebSocketClientProtocolHandler(
WebSocketClientProtocolConfig.newBuilder()
.webSocketUri(getWebsocketUri())
.version(WebSocketVersion.V13)
.subprotocol(null)
.allowExtensions(true)
.customHeaders(headers)
.maxFramePayloadLength(getConfig().getMaxFramePayloadLength())
.handshakeTimeoutMillis(getConfig().getHandshakeTimeoutMillis())
.build()
);
},
BilibiliLiveChatClient.this, clientConnectionListener
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import tech.ordinaryroad.live.chat.client.codec.bilibili.msg.base.BaseBilibiliMsg;
import tech.ordinaryroad.live.chat.client.codec.bilibili.msg.base.IBilibiliMsg;
import tech.ordinaryroad.live.chat.client.commons.base.msg.ICmdMsg;
import tech.ordinaryroad.live.chat.client.commons.base.msg.IMsg;
import tech.ordinaryroad.live.chat.client.servers.netty.client.handler.BaseNettyClientBinaryFrameHandler;

import java.util.List;
Expand Down Expand Up @@ -68,6 +69,20 @@ public BilibiliBinaryFrameHandler(List<IBilibiliMsgListener> msgListeners, long
this.roomIdPair = roomIdPair;
}

@Override
public void onUnknownCmd(BilibiliBinaryFrameHandler binaryFrameHandler, String cmdString, IMsg msg) {
if (cmdString.startsWith("DANMU_MSG")) {
MessageMsg messageMsg = (MessageMsg) msg;
DanmuMsgMsg danmuMsgMsg = new DanmuMsgMsg();
danmuMsgMsg.setProtover(messageMsg.getProtover());
danmuMsgMsg.setInfo(messageMsg.getInfo());
danmuMsgMsg.setDm_v2(StrUtil.toStringOrNull(messageMsg.getUnknownProperties().get("dm_v2")));
iteratorMsgListeners(msgListener -> msgListener.onDanmuMsg(BilibiliBinaryFrameHandler.this, danmuMsgMsg));
} else {
super.onUnknownCmd(binaryFrameHandler, cmdString, msg);
}
}

@SneakyThrows
@Override
public void onCmdMsg(BilibiliCmdEnum cmd, ICmdMsg<BilibiliCmdEnum> cmdMsg) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public void sendHeartbeat(Channel channel) {
if (log.isDebugEnabled()) {
log.debug("发送心跳包");
}
channel.writeAndFlush(getWebSocketFrameFactory(getRoomId()).createHeartbeat(getProtover()))
channel.writeAndFlush(getWebSocketFrameFactory(getRoomId()).createHeartbeat(ProtoverEnum.HEARTBEAT_AUTH_NO_COMPRESSION))
.addListener((ChannelFutureListener) future -> {
if (future.isSuccess()) {
if (log.isDebugEnabled()) {
Expand All @@ -127,7 +127,7 @@ public void sendAuthRequest(Channel channel) {
if (log.isDebugEnabled()) {
log.debug("发送认证包");
}
channel.writeAndFlush(getWebSocketFrameFactory(getRoomId()).createAuth(getProtover(), roomInitResult))
channel.writeAndFlush(getWebSocketFrameFactory(getRoomId()).createAuth(ProtoverEnum.HEARTBEAT_AUTH_NO_COMPRESSION, roomInitResult))
.addListener(future -> {
if (future.isSuccess()) {
if (log.isDebugEnabled()) {
Expand Down

0 comments on commit cc99f44

Please sign in to comment.