diff --git a/README.md b/README.md
index ee361db..137f3e1 100644
--- a/README.md
+++ b/README.md
@@ -13,15 +13,7 @@
# 添加依赖
## Gradle
- compile group: 'com.hiczp', name: 'bilibili-api', version: '0.0.3'
-
-## Maven
-
-
- com.hiczp
- bilibili-api
- 0.0.3
-
+ compile group: 'com.hiczp', name: 'bilibili-api', version: '0.0.4'
# 名词解释
B站不少参数都是瞎取的, 并且不统一, 经常混用, 以下给出一些常见参数对应的含义
diff --git a/src/main/java/com/hiczp/bilibili/api/live/bulletScreen/BulletScreenConstDefinition.java b/src/main/java/com/hiczp/bilibili/api/live/bulletScreen/BulletScreenConstDefinition.java
new file mode 100644
index 0000000..eff2950
--- /dev/null
+++ b/src/main/java/com/hiczp/bilibili/api/live/bulletScreen/BulletScreenConstDefinition.java
@@ -0,0 +1,5 @@
+package com.hiczp.bilibili.api.live.bulletScreen;
+
+public class BulletScreenConstDefinition {
+ public static final int DEFAULT_MESSAGE_LENGTH_LIMIT = 20;
+}
diff --git a/src/main/java/com/hiczp/bilibili/api/live/bulletScreen/BulletScreenHelper.java b/src/main/java/com/hiczp/bilibili/api/live/bulletScreen/BulletScreenHelper.java
new file mode 100644
index 0000000..6bdb5a3
--- /dev/null
+++ b/src/main/java/com/hiczp/bilibili/api/live/bulletScreen/BulletScreenHelper.java
@@ -0,0 +1,17 @@
+package com.hiczp.bilibili.api.live.bulletScreen;
+
+import javax.annotation.Nonnull;
+
+public class BulletScreenHelper {
+ public static String[] splitMessageByFixedLength(@Nonnull String message, int lengthLimit) {
+ int count = message.length() / lengthLimit;
+ if (message.length() % lengthLimit != 0) {
+ count++;
+ }
+ String[] messages = new String[count];
+ for (int i = 0; i < count; i++) {
+ messages[i] = message.substring(i * lengthLimit, i != count - 1 ? (i + 1) * lengthLimit : message.length());
+ }
+ return messages;
+ }
+}
diff --git a/src/main/java/com/hiczp/bilibili/api/live/bulletScreen/BulletScreenSendingCallback.java b/src/main/java/com/hiczp/bilibili/api/live/bulletScreen/BulletScreenSendingCallback.java
new file mode 100644
index 0000000..0171e22
--- /dev/null
+++ b/src/main/java/com/hiczp/bilibili/api/live/bulletScreen/BulletScreenSendingCallback.java
@@ -0,0 +1,10 @@
+package com.hiczp.bilibili.api.live.bulletScreen;
+
+import com.hiczp.bilibili.api.live.entity.BulletScreenEntity;
+import com.hiczp.bilibili.api.live.entity.SendBulletScreenResponseEntity;
+
+public interface BulletScreenSendingCallback {
+ void onResponse(BulletScreenEntity bulletScreenEntity, SendBulletScreenResponseEntity sendBulletScreenResponseEntity);
+
+ void onFailure(BulletScreenEntity bulletScreenEntity, Throwable throwable);
+}
diff --git a/src/main/java/com/hiczp/bilibili/api/live/bulletScreen/BulletScreenSendingTask.java b/src/main/java/com/hiczp/bilibili/api/live/bulletScreen/BulletScreenSendingTask.java
new file mode 100644
index 0000000..81c4084
--- /dev/null
+++ b/src/main/java/com/hiczp/bilibili/api/live/bulletScreen/BulletScreenSendingTask.java
@@ -0,0 +1,28 @@
+package com.hiczp.bilibili.api.live.bulletScreen;
+
+import com.hiczp.bilibili.api.BilibiliServiceProvider;
+import com.hiczp.bilibili.api.live.entity.BulletScreenEntity;
+
+public class BulletScreenSendingTask {
+ private BilibiliServiceProvider bilibiliServiceProvider;
+ private BulletScreenEntity bulletScreenEntity;
+ private BulletScreenSendingCallback bulletScreenSendingCallback;
+
+ public BulletScreenSendingTask(BilibiliServiceProvider bilibiliServiceProvider, BulletScreenEntity bulletScreenEntity, BulletScreenSendingCallback bulletScreenSendingCallback) {
+ this.bilibiliServiceProvider = bilibiliServiceProvider;
+ this.bulletScreenEntity = bulletScreenEntity;
+ this.bulletScreenSendingCallback = bulletScreenSendingCallback;
+ }
+
+ public BilibiliServiceProvider getBilibiliServiceProvider() {
+ return bilibiliServiceProvider;
+ }
+
+ public BulletScreenEntity getBulletScreenEntity() {
+ return bulletScreenEntity;
+ }
+
+ public BulletScreenSendingCallback getBulletScreenSendingCallback() {
+ return bulletScreenSendingCallback;
+ }
+}
diff --git a/src/main/java/com/hiczp/bilibili/api/live/socket/LiveClient.java b/src/main/java/com/hiczp/bilibili/api/live/socket/LiveClient.java
index f86b715..8509a35 100644
--- a/src/main/java/com/hiczp/bilibili/api/live/socket/LiveClient.java
+++ b/src/main/java/com/hiczp/bilibili/api/live/socket/LiveClient.java
@@ -3,6 +3,7 @@ package com.hiczp.bilibili.api.live.socket;
import com.google.common.eventbus.EventBus;
import com.google.common.eventbus.Subscribe;
import com.hiczp.bilibili.api.BilibiliServiceProvider;
+import com.hiczp.bilibili.api.live.bulletScreen.BulletScreenConstDefinition;
import com.hiczp.bilibili.api.live.entity.BulletScreenEntity;
import com.hiczp.bilibili.api.live.entity.LiveRoomInfoEntity;
import com.hiczp.bilibili.api.live.entity.SendBulletScreenResponseEntity;
@@ -22,6 +23,7 @@ import io.netty.handler.timeout.IdleStateHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import javax.annotation.Nonnull;
import java.io.Closeable;
import java.io.IOException;
import java.util.Optional;
@@ -29,7 +31,7 @@ import java.util.Optional;
public class LiveClient implements Closeable {
private static final Logger LOGGER = LoggerFactory.getLogger(LiveClient.class);
- private final EventBus eventBus = new EventBus("BilibiliLiveClient");
+ private final EventBus eventBus = new EventBus("BilibiliLiveClientEventBus");
private final BilibiliServiceProvider bilibiliServiceProvider;
private final long showRoomId;
private final long userId;
@@ -43,20 +45,28 @@ public class LiveClient implements Closeable {
eventBus.register(new ConnectionCloseListener());
}
- public LiveClient(BilibiliServiceProvider bilibiliServiceProvider, long showRoomId) {
+ public LiveClient(@Nonnull BilibiliServiceProvider bilibiliServiceProvider, long showRoomId) {
this.bilibiliServiceProvider = bilibiliServiceProvider;
this.showRoomId = showRoomId;
this.userId = 0;
initEventBus();
}
- public LiveClient(BilibiliServiceProvider bilibiliServiceProvider, long showRoomId, long userId) {
+ public LiveClient(@Nonnull BilibiliServiceProvider bilibiliServiceProvider, long showRoomId, long userId) {
this.bilibiliServiceProvider = bilibiliServiceProvider;
this.showRoomId = showRoomId;
this.userId = userId;
initEventBus();
}
+ public LiveRoomInfoEntity.LiveRoomEntity fetchRoomInfo() throws IOException {
+ return bilibiliServiceProvider.getLiveService()
+ .getRoomInfo(showRoomId)
+ .execute()
+ .body()
+ .getData();
+ }
+
public synchronized LiveClient connect() throws IOException {
if (channel != null && channel.isActive()) {
LOGGER.warn("Already connected to server, connect method can not be invoked twice");
@@ -68,11 +78,7 @@ public class LiveClient implements Closeable {
}
LOGGER.info("Fetching info of live room {}", showRoomId);
- liveRoomEntity = bilibiliServiceProvider.getLiveService()
- .getRoomInfo(showRoomId)
- .execute()
- .body()
- .getData();
+ liveRoomEntity = fetchRoomInfo();
long roomId = liveRoomEntity.getRoomId();
LOGGER.info("Get actual room id {}", roomId);
@@ -141,36 +147,49 @@ public class LiveClient implements Closeable {
return eventBus;
}
- public LiveClient registerListener(Object object) {
+ public LiveClient registerListener(@Nonnull Object object) {
eventBus.register(object);
return this;
}
- public LiveClient unregisterListeners(Object object) {
+ public LiveClient unregisterListeners(@Nonnull Object object) {
eventBus.unregister(object);
return this;
}
- public SendBulletScreenResponseEntity sendBulletScreen(String message) throws IOException {
- return sendBulletScreen(
- new BulletScreenEntity(
- liveRoomEntity != null ? liveRoomEntity.getRoomId() : showRoomId,
- userId,
- message
- )
- );
- }
-
- public SendBulletScreenResponseEntity sendBulletScreen(BulletScreenEntity bulletScreenEntity) throws IOException {
+ public SendBulletScreenResponseEntity sendBulletScreen(@Nonnull String message) throws IOException {
return bilibiliServiceProvider.getLiveService()
- .sendBulletScreen(bulletScreenEntity)
+ .sendBulletScreen(createBulletScreenEntity(message))
.execute()
.body();
}
- //TODO 弹幕发送队列
- public void sendBulletScreenInBlockingQueue(String message) {
- throw new UnsupportedOperationException();
+// public void sendBulletScreenAsync(@Nonnull String message, @Nonnull BulletScreenSendingCallback bulletScreenSendingCallback, boolean autoSplit) {
+// if (!autoSplit) {
+// sendBulletScreenAsync(message, bulletScreenSendingCallback);
+// } else {
+// for (String s : BulletScreenHelper.splitMessageByFixedLength(message, getBulletScreenLengthLimitOrDefaultLengthLimit())) {
+// sendBulletScreenAsync(s, bulletScreenSendingCallback);
+// }
+// }
+// }
+//
+// public void sendBulletScreenAsync(@Nonnull String message, @Nonnull BulletScreenSendingCallback bulletScreenSendingCallback) {
+// BulletScreenSendingDequeHolder.addTask(
+// new BulletScreenSendingTask(
+// bilibiliServiceProvider,
+// createBulletScreenEntity(message),
+// bulletScreenSendingCallback
+// )
+// );
+// }
+
+ private BulletScreenEntity createBulletScreenEntity(String message) {
+ return new BulletScreenEntity(
+ getRoomIdOrShowRoomId(),
+ userId,
+ message
+ );
}
public long getShowRoomId() {
@@ -182,6 +201,21 @@ public class LiveClient implements Closeable {
}
public Optional getRoomInfo() {
+ if (liveRoomEntity == null) {
+ try {
+ liveRoomEntity = fetchRoomInfo();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
return Optional.of(liveRoomEntity);
}
+
+ public long getRoomIdOrShowRoomId() {
+ return getRoomInfo().map(LiveRoomInfoEntity.LiveRoomEntity::getRoomId).orElse(showRoomId);
+ }
+
+ public int getBulletScreenLengthLimitOrDefaultLengthLimit() {
+ return getRoomInfo().map(LiveRoomInfoEntity.LiveRoomEntity::getMsgLength).orElse(BulletScreenConstDefinition.DEFAULT_MESSAGE_LENGTH_LIMIT);
+ }
}