diff --git a/src/main/java/com/yutou/biliapi/net/WebSocketServer.java b/src/main/java/com/yutou/biliapi/net/WebSocketServer.java index dd16e6d..46cc5aa 100644 --- a/src/main/java/com/yutou/biliapi/net/WebSocketServer.java +++ b/src/main/java/com/yutou/biliapi/net/WebSocketServer.java @@ -18,6 +18,7 @@ import com.yutou.biliapi.databases.BiliBiliLoginDatabase; import com.yutou.biliapi.databases.BiliLiveDatabase; import com.yutou.biliapi.utils.BytesUtils; import com.yutou.bilibili.services.LiveDatabasesService; +import com.yutou.bilibili.services.LiveService; import com.yutou.common.okhttp.HttpBody; import com.yutou.common.okhttp.HttpCallback; import com.yutou.common.utils.ConfigTools; @@ -70,16 +71,16 @@ public class WebSocketServer { } public void addRoom(LiveRoomConfig roomConfig, boolean isUser) { - if (!isUser && userStopList.contains(roomConfig.getRoomId().toString())) { + if (!isUser && userStopList.contains(roomConfig.getRoomId())) { return; } if (checkRoom(roomConfig)) { return; } if (isUser) { - userStopList.remove(roomConfig.getRoomId().toString()); + userStopList.remove(roomConfig.getRoomId()); } - DanmuTask task = new DanmuTask(roomConfig); + DanmuTask task = new DanmuTask(roomConfig, isUser); roomMap.put(roomConfig, task); Log.i("添加websocket任务"); executor.execute(task); @@ -105,12 +106,14 @@ public class WebSocketServer { return userStopList.contains(roomId); } - private class DanmuTask implements Runnable { + private class DanmuTask implements Runnable { LiveRoomConfig roomConfig; WebSocketClientTh client; + boolean isUser; - public DanmuTask(LiveRoomConfig config) { + public DanmuTask(LiveRoomConfig config, boolean isUser) { this.roomConfig = config; + this.isUser = isUser; roomMap.put(roomConfig, this); } @@ -136,7 +139,7 @@ public class WebSocketServer { // url="ws://127.0.0.1:8765"; try { roomConfig.setLiveInfo(response); - client = new WebSocketClientTh(new URI(url), roomConfig); + client = new WebSocketClientTh(new URI(url), roomConfig, isUser); } catch (URISyntaxException e) { roomMap.remove(roomConfig); throw new RuntimeException(e); @@ -161,10 +164,12 @@ public class WebSocketServer { private final LiveRoomConfig roomConfig; private final HeartbeatTask heartbeatTask; BiliLiveDatabase liveDatabase; + private final boolean isUser; - public WebSocketClientTh(URI serverUri, LiveRoomConfig roomId) { + public WebSocketClientTh(URI serverUri, LiveRoomConfig roomId, boolean isUser) { super(serverUri); Log.i("WebSocketClientTh.WebSocketClientTh : " + serverUri); + this.isUser = isUser; this.roomConfig = roomId; liveDatabase = liveDatabasesService.getLiveDatabase(roomConfig.getRoomId()); Brotli4jLoader.ensureAvailability(); @@ -199,6 +204,11 @@ public class WebSocketServer { Log.e("WebSocketClientTh.onClose", "i = " + i + ", s = " + s + ", b = " + b, roomConfig.getRoomId(), heartbeatTask.socket.isOpen()); roomMap.remove(roomConfig); heartbeatTask.cancel(); + if (i == 1006) { + if (LiveService.checkLive(roomConfig.getRoomId())) { + addRoom(roomConfig, isUser); + } + } } @Override diff --git a/src/main/java/com/yutou/bilibili/services/LiveService.java b/src/main/java/com/yutou/bilibili/services/LiveService.java index c946214..e46190c 100644 --- a/src/main/java/com/yutou/bilibili/services/LiveService.java +++ b/src/main/java/com/yutou/bilibili/services/LiveService.java @@ -4,6 +4,7 @@ import com.alibaba.fastjson2.JSONArray; import com.alibaba.fastjson2.JSONObject; import com.yutou.biliapi.api.LiveApi; import com.yutou.biliapi.bean.live.LiveAnchorInfo; +import com.yutou.biliapi.bean.live.LiveRoomInfo; import com.yutou.biliapi.bean.live.database.*; import com.yutou.biliapi.databases.BiliLiveDatabase; import com.yutou.biliapi.net.BiliLiveNetApiManager; @@ -12,6 +13,7 @@ import com.yutou.bilibili.datas.web.LiveData; import com.yutou.bilibili.datas.web.LiveWebDanmuBean; import com.yutou.bilibili.datas.web.SuperChatGiftBean; import com.yutou.common.okhttp.HttpLoggingInterceptor; +import com.yutou.common.utils.Log; import jakarta.annotation.Resource; import org.springframework.stereotype.Service; import org.springframework.util.StringUtils; @@ -110,9 +112,9 @@ public class LiveService { long startTime = videoBean.getStartTime().getTime(); long endTime = videoBean.getStopTime() == null ? System.currentTimeMillis() : videoBean.getStopTime().getTime(); List superChatList = database.getOfTime(startTime, endTime, LiveSuperChatDatabaseBean.class) - .stream().map(it-> new SuperChatGiftBean(startTime,it)).toList(); + .stream().map(it -> new SuperChatGiftBean(startTime, it)).toList(); JSONObject giftInfo = database.getGiftInfo(startTime, endTime); - giftInfo.put("superChat",superChatList); + giftInfo.put("superChat", superChatList); return giftInfo; } @@ -132,8 +134,8 @@ public class LiveService { JSONArray array = new JSONArray(); array.addAll(list); array.addAll( - list2.stream().map(it->{ - LiveGiftDatabaseBean tmp=new LiveGiftDatabaseBean(); + list2.stream().map(it -> { + LiveGiftDatabaseBean tmp = new LiveGiftDatabaseBean(); tmp.setGiftId(it.getGiftID()); tmp.setGiftName(it.getGiftName()); tmp.setGiftNum(it.getNum()); @@ -151,6 +153,16 @@ public class LiveService { } + public static boolean checkLive(String roomId) { + try { + LiveRoomInfo data = BiliLiveNetApiManager.getInstance().getApi(null).getRoomInfo(roomId).execute().body().getData(); + return data.getLiveStatus() == 1; + } catch (IOException|NullPointerException e) { + Log.e(e); + return false; + } + } + public static void main(String[] args) { HttpLoggingInterceptor.setLog(true); LiveService service = new LiveService(); diff --git a/src/main/java/com/yutou/common/utils/Log.java b/src/main/java/com/yutou/common/utils/Log.java index fcaacc7..dab9d16 100644 --- a/src/main/java/com/yutou/common/utils/Log.java +++ b/src/main/java/com/yutou/common/utils/Log.java @@ -1,10 +1,7 @@ package com.yutou.common.utils; -import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Marker; -import org.apache.logging.log4j.MarkerManager; import org.apache.logging.log4j.util.StackLocatorUtil;