调整websocket断开后重连

This commit is contained in:
zlzw 2024-12-02 13:54:01 +08:00
parent e089c57c01
commit 0497e9d0e2
3 changed files with 33 additions and 14 deletions

View File

@ -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);
@ -108,9 +109,11 @@ public class WebSocketServer {
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

View File

@ -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;
@ -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();

View File

@ -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;