现在通过 LiveClient 可以直接发送弹幕

This commit is contained in:
czp 2018-02-06 15:25:45 +08:00
parent 34e7ca5043
commit c4d2a9a870

View File

@ -3,7 +3,9 @@ package com.hiczp.bilibili.api.live.socket;
import com.google.common.eventbus.EventBus; import com.google.common.eventbus.EventBus;
import com.google.common.eventbus.Subscribe; import com.google.common.eventbus.Subscribe;
import com.hiczp.bilibili.api.BilibiliServiceProvider; import com.hiczp.bilibili.api.BilibiliServiceProvider;
import com.hiczp.bilibili.api.live.entity.BulletScreenEntity;
import com.hiczp.bilibili.api.live.entity.LiveRoomInfoEntity; import com.hiczp.bilibili.api.live.entity.LiveRoomInfoEntity;
import com.hiczp.bilibili.api.live.entity.SendBulletScreenResponseEntity;
import com.hiczp.bilibili.api.live.socket.codec.PackageDecoder; import com.hiczp.bilibili.api.live.socket.codec.PackageDecoder;
import com.hiczp.bilibili.api.live.socket.codec.PackageEncoder; import com.hiczp.bilibili.api.live.socket.codec.PackageEncoder;
import com.hiczp.bilibili.api.live.socket.event.ConnectionCloseEvent; import com.hiczp.bilibili.api.live.socket.event.ConnectionCloseEvent;
@ -32,7 +34,7 @@ public class LiveClient implements Closeable {
private final long showRoomId; private final long showRoomId;
private final long userId; private final long userId;
private Long roomId; private LiveRoomInfoEntity.LiveRoomEntity liveRoomEntity;
private EventLoopGroup eventLoopGroup; private EventLoopGroup eventLoopGroup;
private Channel channel; private Channel channel;
@ -66,12 +68,12 @@ public class LiveClient implements Closeable {
} }
LOGGER.info("Fetching info of live room {}", showRoomId); LOGGER.info("Fetching info of live room {}", showRoomId);
LiveRoomInfoEntity.LiveRoomEntity liveRoomEntity = bilibiliServiceProvider.getLiveService() liveRoomEntity = bilibiliServiceProvider.getLiveService()
.getRoomInfo(showRoomId) .getRoomInfo(showRoomId)
.execute() .execute()
.body() .body()
.getData(); .getData();
roomId = liveRoomEntity.getRoomId(); long roomId = liveRoomEntity.getRoomId();
LOGGER.info("Get actual room id {}", roomId); LOGGER.info("Get actual room id {}", roomId);
eventLoopGroup = new NioEventLoopGroup(1); eventLoopGroup = new NioEventLoopGroup(1);
@ -91,7 +93,6 @@ public class LiveClient implements Closeable {
0 0
)) ))
.addLast(new IdleStateHandler(40, 0, 0)) .addLast(new IdleStateHandler(40, 0, 0))
//.addLast(new LoggingHandler(LogLevel.DEBUG))
.addLast(new PackageEncoder()) .addLast(new PackageEncoder())
.addLast(new PackageDecoder()) .addLast(new PackageDecoder())
.addLast(new LiveClientHandler(roomId, userId, eventBus)); .addLast(new LiveClientHandler(roomId, userId, eventBus));
@ -148,6 +149,24 @@ public class LiveClient implements Closeable {
return this; return this;
} }
public SendBulletScreenResponseEntity sendBulletScreen(String message) throws IOException {
return bilibiliServiceProvider.getLiveService()
.sendBulletScreen(
new BulletScreenEntity(
liveRoomEntity != null ? liveRoomEntity.getRoomId() : showRoomId,
userId,
message
)
)
.execute()
.body();
}
//TODO 弹幕发送队列
public void sendBulletScreenInBlockingQueue(String message) {
throw new UnsupportedOperationException();
}
public long getShowRoomId() { public long getShowRoomId() {
return showRoomId; return showRoomId;
} }
@ -156,7 +175,7 @@ public class LiveClient implements Closeable {
return userId; return userId;
} }
public Optional<Long> getRoomId() { public Optional<LiveRoomInfoEntity.LiveRoomEntity> getRoomInfo() {
return Optional.of(roomId); return Optional.of(liveRoomEntity);
} }
} }