123
This commit is contained in:
@@ -21,6 +21,7 @@ public class AmqpWsProducer {
|
||||
});
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public void sendRoomSettleDelay(String message, Integer timeout){
|
||||
rabbitTemplate.convertAndSend(RoomSettleDelayWsMqConstant.EXCHANGE_NAME,
|
||||
RoomSettleDelayWsMqConstant.ROUTING_KEY,
|
||||
|
||||
@@ -42,8 +42,14 @@ public class Agora {
|
||||
if(jsonobject == null){
|
||||
return Collections.emptyList();
|
||||
}
|
||||
JSONArray jsonArray = jsonobject.getJSONObject("data").getJSONArray("broadcasters");
|
||||
return jsonArray.toJavaList(String.class);
|
||||
JSONObject data = jsonobject.getJSONObject("data");
|
||||
if(data != null){
|
||||
JSONArray jsonArray = data.getJSONArray("broadcasters");
|
||||
if(jsonArray != null){
|
||||
return jsonArray.toJavaList(String.class);
|
||||
}
|
||||
}
|
||||
return new ArrayList<>();
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -119,6 +119,13 @@ public class RoomWebSocketHandler extends AbstractWebSocketHandler {
|
||||
@Override
|
||||
public void handleTransportError(WebSocketSession session, Throwable exception) throws Exception {
|
||||
log.error("[transport error] sessionId: {} , exception:{}", session.getId(), exception.getMessage());
|
||||
try{
|
||||
if(session.isOpen()){
|
||||
session.close();
|
||||
}
|
||||
}catch (Exception e){
|
||||
log.error(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -133,7 +140,13 @@ public class RoomWebSocketHandler extends AbstractWebSocketHandler {
|
||||
// TODO 连接关闭 是否要删除fd的关系
|
||||
WebSocketSessionHolder.removeSession(session.getId());
|
||||
log.info("[disconnect] sessionId: {},token:{}", session.getId(), token);
|
||||
session.close();
|
||||
try{
|
||||
if(session.isOpen()){
|
||||
session.close();
|
||||
}
|
||||
}catch (Exception e){
|
||||
log.error(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -4,6 +4,7 @@ import lombok.AccessLevel;
|
||||
import lombok.NoArgsConstructor;
|
||||
import org.springframework.web.socket.WebSocketSession;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
@@ -17,19 +18,36 @@ import java.util.concurrent.ConcurrentHashMap;
|
||||
public class WebSocketSessionHolder {
|
||||
|
||||
private static final Map<String, WebSocketSession> USER_SESSION_MAP = new ConcurrentHashMap<>();
|
||||
private static final Map<Long, String> USER_SESSION_ID_MAP = new ConcurrentHashMap<>();
|
||||
|
||||
public static int size(){
|
||||
return USER_SESSION_MAP.size();
|
||||
}
|
||||
|
||||
public static void addSession(String sessionKey, WebSocketSession session) {
|
||||
public static void addSession(String sessionKey, WebSocketSession session,Long userId) {
|
||||
USER_SESSION_MAP.put(sessionKey, session);
|
||||
if(USER_SESSION_ID_MAP.containsKey(userId)){ // T人动作
|
||||
String sessionKeyOld = USER_SESSION_ID_MAP.get(userId);
|
||||
if(sessionKeyOld != null){
|
||||
WebSocketSession webSocketSession = USER_SESSION_MAP.get(sessionKeyOld);
|
||||
if(webSocketSession != null){
|
||||
if(webSocketSession.isOpen()){
|
||||
try {
|
||||
webSocketSession.close();
|
||||
} catch (IOException e) {
|
||||
// ignore
|
||||
}
|
||||
}
|
||||
removeSession(sessionKeyOld);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
USER_SESSION_ID_MAP.put(userId,sessionKey);
|
||||
}
|
||||
|
||||
public static void removeSession(String sessionKey) {
|
||||
if (USER_SESSION_MAP.containsKey(sessionKey)) {
|
||||
USER_SESSION_MAP.remove(sessionKey);
|
||||
}
|
||||
USER_SESSION_MAP.remove(sessionKey);
|
||||
}
|
||||
|
||||
public static WebSocketSession getSessions(String sessionKey) {
|
||||
|
||||
@@ -29,6 +29,7 @@ public class PlusWebSocketInterceptor implements HandshakeInterceptor {
|
||||
*/
|
||||
@Override
|
||||
public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Map<String, Object> attributes) {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -21,6 +21,8 @@ import org.redisson.api.RedissonClient;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@Component
|
||||
public class WebSocketManager {
|
||||
@Autowired
|
||||
@@ -64,14 +66,21 @@ public class WebSocketManager {
|
||||
return room.getRoomId();
|
||||
}
|
||||
RLock lock = redissonClient.getLock(String.format(RedisConstant.INIT_ROOM_LOCK, userCall.getFromUid(), userCall.getToUid()));
|
||||
boolean b = lock.tryLock();
|
||||
if(!b){
|
||||
throw new ServiceException("系统繁忙");
|
||||
}
|
||||
boolean b;
|
||||
try {
|
||||
b = lock.tryLock(1, TimeUnit.MINUTES);
|
||||
if(!b){
|
||||
throw new ServiceException("系统繁忙");
|
||||
}
|
||||
roomId = initRoom(userCall);
|
||||
}finally {
|
||||
lock.unlock();
|
||||
} catch (InterruptedException e) {
|
||||
throw new ServiceException("系统繁忙");
|
||||
} finally {
|
||||
try {
|
||||
lock.unlock();
|
||||
}catch (Exception e){
|
||||
// ignore
|
||||
}
|
||||
}
|
||||
return roomId;
|
||||
}
|
||||
|
||||
@@ -71,7 +71,7 @@ public class OpenLogic {
|
||||
WsExceptionUtil.throwExceptionFast(session,"无效token");
|
||||
return;
|
||||
}
|
||||
WebSocketSessionHolder.addSession(session.getId(), session);
|
||||
WebSocketSessionHolder.addSession(session.getId(), session, wsToken.getUserId());
|
||||
Long roomId = wsToken.getRoomId();
|
||||
Long userId = wsToken.getUserId();
|
||||
Room room = roomService.load(roomId);
|
||||
|
||||
@@ -27,6 +27,7 @@ import org.springframework.transaction.annotation.Transactional;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@Component
|
||||
@Slf4j
|
||||
@@ -99,7 +100,7 @@ public class SettleService {
|
||||
/**
|
||||
* 结算处理
|
||||
*/
|
||||
public SettleResp processOn(Long roomId){
|
||||
public SettleResp processOn(Long roomId) {
|
||||
Room room = roomService.load(roomId);
|
||||
if(room == null){
|
||||
log.warn("房间不存在,无需结算 roomId={}",roomId);
|
||||
@@ -112,17 +113,23 @@ public class SettleService {
|
||||
log.info("正在结算中,稍等!");
|
||||
return SettleResp.builder().nextRun(true).build();
|
||||
}
|
||||
boolean lockFlag = clientLock.tryLock();
|
||||
if(!lockFlag){
|
||||
log.info("正在结算中,稍等!");
|
||||
return SettleResp.builder().nextRun(true).build();
|
||||
}
|
||||
boolean lockFlag = false;
|
||||
try {
|
||||
lockFlag = clientLock.tryLock(5,60, TimeUnit.SECONDS);
|
||||
if(!lockFlag){
|
||||
log.info("正在结算中,稍等!");
|
||||
return SettleResp.builder().nextRun(true).build();
|
||||
}
|
||||
return deal(room);
|
||||
}finally {
|
||||
clientLock.unlock();
|
||||
} catch (InterruptedException e) {
|
||||
throw new RuntimeException(e);
|
||||
} finally {
|
||||
try {
|
||||
clientLock.unlock();
|
||||
}catch (Exception e){
|
||||
// ignore
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Autowired
|
||||
|
||||
@@ -2,22 +2,41 @@ package com.ruoyi.cai.ws.util;
|
||||
|
||||
import com.alibaba.fastjson2.JSON;
|
||||
import com.ruoyi.cai.ws.dto.WsR;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.socket.WebSocketSession;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import java.util.List;
|
||||
|
||||
@Component
|
||||
public class RoomWebSocketUtil {
|
||||
|
||||
@Autowired
|
||||
private SendMessageI sendMessage;
|
||||
|
||||
private static SendMessageI sendMessageStatic;
|
||||
|
||||
@PostConstruct
|
||||
public void init(){
|
||||
sendMessageStatic = sendMessage;
|
||||
}
|
||||
|
||||
|
||||
public static void sendSendMessage(String sessionKey, WsR r){
|
||||
WebSocketUtils.sendMessage(sessionKey, JSON.toJSONString(r));
|
||||
sendMessageStatic.sendMessage(sessionKey, JSON.toJSONString(r));
|
||||
// WebSocketUtils.sendMessage(sessionKey, JSON.toJSONString(r));
|
||||
}
|
||||
|
||||
public static void sendSendMessage(WebSocketSession sessionKey, WsR r){
|
||||
WebSocketUtils.sendMessage(sessionKey, JSON.toJSONString(r));
|
||||
sendMessageStatic.sendMessage(sessionKey.getId(), JSON.toJSONString(r));
|
||||
// WebSocketUtils.sendMessage(sessionKey, JSON.toJSONString(r));
|
||||
}
|
||||
|
||||
public static void sendSendMessage(List<String> sessionKey, WsR r){
|
||||
for (String s : sessionKey) {
|
||||
WebSocketUtils.sendMessage(s, JSON.toJSONString(r));
|
||||
sendMessageStatic.sendMessage(s, JSON.toJSONString(r));
|
||||
// WebSocketUtils.sendMessage(s, JSON.toJSONString(r));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
package com.ruoyi.cai.ws.util;
|
||||
|
||||
|
||||
public interface SendMessageI {
|
||||
void sendMessage(String sessionKey, String message);
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.ruoyi.cai.ws.util;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class SessionObj {
|
||||
private String sessionKey;
|
||||
private String data;
|
||||
}
|
||||
@@ -45,7 +45,9 @@ public class WebSocketUtils {
|
||||
public static boolean close(WebSocketSession session) {
|
||||
if(session != null){
|
||||
try {
|
||||
session.close();
|
||||
if(session.isOpen()){
|
||||
session.close();
|
||||
}
|
||||
return true;
|
||||
} catch (IOException e) {
|
||||
log.error("关闭ws失败,session={}",session,e);
|
||||
|
||||
Reference in New Issue
Block a user