调整websocket断开后重连
This commit is contained in:
parent
e089c57c01
commit
0497e9d0e2
@ -18,6 +18,7 @@ import com.yutou.biliapi.databases.BiliBiliLoginDatabase;
|
|||||||
import com.yutou.biliapi.databases.BiliLiveDatabase;
|
import com.yutou.biliapi.databases.BiliLiveDatabase;
|
||||||
import com.yutou.biliapi.utils.BytesUtils;
|
import com.yutou.biliapi.utils.BytesUtils;
|
||||||
import com.yutou.bilibili.services.LiveDatabasesService;
|
import com.yutou.bilibili.services.LiveDatabasesService;
|
||||||
|
import com.yutou.bilibili.services.LiveService;
|
||||||
import com.yutou.common.okhttp.HttpBody;
|
import com.yutou.common.okhttp.HttpBody;
|
||||||
import com.yutou.common.okhttp.HttpCallback;
|
import com.yutou.common.okhttp.HttpCallback;
|
||||||
import com.yutou.common.utils.ConfigTools;
|
import com.yutou.common.utils.ConfigTools;
|
||||||
@ -70,16 +71,16 @@ public class WebSocketServer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void addRoom(LiveRoomConfig roomConfig, boolean isUser) {
|
public void addRoom(LiveRoomConfig roomConfig, boolean isUser) {
|
||||||
if (!isUser && userStopList.contains(roomConfig.getRoomId().toString())) {
|
if (!isUser && userStopList.contains(roomConfig.getRoomId())) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (checkRoom(roomConfig)) {
|
if (checkRoom(roomConfig)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (isUser) {
|
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);
|
roomMap.put(roomConfig, task);
|
||||||
Log.i("添加websocket任务");
|
Log.i("添加websocket任务");
|
||||||
executor.execute(task);
|
executor.execute(task);
|
||||||
@ -105,12 +106,14 @@ public class WebSocketServer {
|
|||||||
return userStopList.contains(roomId);
|
return userStopList.contains(roomId);
|
||||||
}
|
}
|
||||||
|
|
||||||
private class DanmuTask implements Runnable {
|
private class DanmuTask implements Runnable {
|
||||||
LiveRoomConfig roomConfig;
|
LiveRoomConfig roomConfig;
|
||||||
WebSocketClientTh client;
|
WebSocketClientTh client;
|
||||||
|
boolean isUser;
|
||||||
|
|
||||||
public DanmuTask(LiveRoomConfig config) {
|
public DanmuTask(LiveRoomConfig config, boolean isUser) {
|
||||||
this.roomConfig = config;
|
this.roomConfig = config;
|
||||||
|
this.isUser = isUser;
|
||||||
roomMap.put(roomConfig, this);
|
roomMap.put(roomConfig, this);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -136,7 +139,7 @@ public class WebSocketServer {
|
|||||||
// url="ws://127.0.0.1:8765";
|
// url="ws://127.0.0.1:8765";
|
||||||
try {
|
try {
|
||||||
roomConfig.setLiveInfo(response);
|
roomConfig.setLiveInfo(response);
|
||||||
client = new WebSocketClientTh(new URI(url), roomConfig);
|
client = new WebSocketClientTh(new URI(url), roomConfig, isUser);
|
||||||
} catch (URISyntaxException e) {
|
} catch (URISyntaxException e) {
|
||||||
roomMap.remove(roomConfig);
|
roomMap.remove(roomConfig);
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
@ -161,10 +164,12 @@ public class WebSocketServer {
|
|||||||
private final LiveRoomConfig roomConfig;
|
private final LiveRoomConfig roomConfig;
|
||||||
private final HeartbeatTask heartbeatTask;
|
private final HeartbeatTask heartbeatTask;
|
||||||
BiliLiveDatabase liveDatabase;
|
BiliLiveDatabase liveDatabase;
|
||||||
|
private final boolean isUser;
|
||||||
|
|
||||||
public WebSocketClientTh(URI serverUri, LiveRoomConfig roomId) {
|
public WebSocketClientTh(URI serverUri, LiveRoomConfig roomId, boolean isUser) {
|
||||||
super(serverUri);
|
super(serverUri);
|
||||||
Log.i("WebSocketClientTh.WebSocketClientTh : " + serverUri);
|
Log.i("WebSocketClientTh.WebSocketClientTh : " + serverUri);
|
||||||
|
this.isUser = isUser;
|
||||||
this.roomConfig = roomId;
|
this.roomConfig = roomId;
|
||||||
liveDatabase = liveDatabasesService.getLiveDatabase(roomConfig.getRoomId());
|
liveDatabase = liveDatabasesService.getLiveDatabase(roomConfig.getRoomId());
|
||||||
Brotli4jLoader.ensureAvailability();
|
Brotli4jLoader.ensureAvailability();
|
||||||
@ -199,6 +204,11 @@ public class WebSocketServer {
|
|||||||
Log.e("WebSocketClientTh.onClose", "i = " + i + ", s = " + s + ", b = " + b, roomConfig.getRoomId(), heartbeatTask.socket.isOpen());
|
Log.e("WebSocketClientTh.onClose", "i = " + i + ", s = " + s + ", b = " + b, roomConfig.getRoomId(), heartbeatTask.socket.isOpen());
|
||||||
roomMap.remove(roomConfig);
|
roomMap.remove(roomConfig);
|
||||||
heartbeatTask.cancel();
|
heartbeatTask.cancel();
|
||||||
|
if (i == 1006) {
|
||||||
|
if (LiveService.checkLive(roomConfig.getRoomId())) {
|
||||||
|
addRoom(roomConfig, isUser);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -4,6 +4,7 @@ import com.alibaba.fastjson2.JSONArray;
|
|||||||
import com.alibaba.fastjson2.JSONObject;
|
import com.alibaba.fastjson2.JSONObject;
|
||||||
import com.yutou.biliapi.api.LiveApi;
|
import com.yutou.biliapi.api.LiveApi;
|
||||||
import com.yutou.biliapi.bean.live.LiveAnchorInfo;
|
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.bean.live.database.*;
|
||||||
import com.yutou.biliapi.databases.BiliLiveDatabase;
|
import com.yutou.biliapi.databases.BiliLiveDatabase;
|
||||||
import com.yutou.biliapi.net.BiliLiveNetApiManager;
|
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.LiveWebDanmuBean;
|
||||||
import com.yutou.bilibili.datas.web.SuperChatGiftBean;
|
import com.yutou.bilibili.datas.web.SuperChatGiftBean;
|
||||||
import com.yutou.common.okhttp.HttpLoggingInterceptor;
|
import com.yutou.common.okhttp.HttpLoggingInterceptor;
|
||||||
|
import com.yutou.common.utils.Log;
|
||||||
import jakarta.annotation.Resource;
|
import jakarta.annotation.Resource;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
@ -110,9 +112,9 @@ public class LiveService {
|
|||||||
long startTime = videoBean.getStartTime().getTime();
|
long startTime = videoBean.getStartTime().getTime();
|
||||||
long endTime = videoBean.getStopTime() == null ? System.currentTimeMillis() : videoBean.getStopTime().getTime();
|
long endTime = videoBean.getStopTime() == null ? System.currentTimeMillis() : videoBean.getStopTime().getTime();
|
||||||
List<SuperChatGiftBean> superChatList = database.getOfTime(startTime, endTime, LiveSuperChatDatabaseBean.class)
|
List<SuperChatGiftBean> 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);
|
JSONObject giftInfo = database.getGiftInfo(startTime, endTime);
|
||||||
giftInfo.put("superChat",superChatList);
|
giftInfo.put("superChat", superChatList);
|
||||||
return giftInfo;
|
return giftInfo;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -132,8 +134,8 @@ public class LiveService {
|
|||||||
JSONArray array = new JSONArray();
|
JSONArray array = new JSONArray();
|
||||||
array.addAll(list);
|
array.addAll(list);
|
||||||
array.addAll(
|
array.addAll(
|
||||||
list2.stream().map(it->{
|
list2.stream().map(it -> {
|
||||||
LiveGiftDatabaseBean tmp=new LiveGiftDatabaseBean();
|
LiveGiftDatabaseBean tmp = new LiveGiftDatabaseBean();
|
||||||
tmp.setGiftId(it.getGiftID());
|
tmp.setGiftId(it.getGiftID());
|
||||||
tmp.setGiftName(it.getGiftName());
|
tmp.setGiftName(it.getGiftName());
|
||||||
tmp.setGiftNum(it.getNum());
|
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) {
|
public static void main(String[] args) {
|
||||||
HttpLoggingInterceptor.setLog(true);
|
HttpLoggingInterceptor.setLog(true);
|
||||||
LiveService service = new LiveService();
|
LiveService service = new LiveService();
|
||||||
|
@ -1,10 +1,7 @@
|
|||||||
package com.yutou.common.utils;
|
package com.yutou.common.utils;
|
||||||
|
|
||||||
|
|
||||||
import org.apache.logging.log4j.Logger;
|
|
||||||
import org.apache.logging.log4j.LogManager;
|
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;
|
import org.apache.logging.log4j.util.StackLocatorUtil;
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user