|
|
|
@ -1,12 +1,14 @@
|
|
|
|
|
package com.supervision.usermanage;
|
|
|
|
|
package com.supervision.websocket;
|
|
|
|
|
|
|
|
|
|
import cn.hutool.core.util.ObjectUtil;
|
|
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
|
|
import cn.hutool.json.JSONUtil;
|
|
|
|
|
import com.baomidou.lock.annotation.Lock4j;
|
|
|
|
|
import com.supervision.constant.UserTokenConstant;
|
|
|
|
|
import com.supervision.controller.WebSocketServer;
|
|
|
|
|
import com.supervision.exception.BusinessException;
|
|
|
|
|
import com.supervision.websocket.dto.KickDTO;
|
|
|
|
|
import com.supervision.websocket.dto.NoResourceDTO;
|
|
|
|
|
import com.supervision.websocket.dto.WebSocketLoginDTO;
|
|
|
|
|
import lombok.Data;
|
|
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
@ -15,12 +17,9 @@ import org.springframework.beans.factory.annotation.Value;
|
|
|
|
|
import javax.websocket.*;
|
|
|
|
|
|
|
|
|
|
import org.springframework.data.redis.core.RedisTemplate;
|
|
|
|
|
import org.springframework.scheduling.annotation.Scheduled;
|
|
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
import java.util.Set;
|
|
|
|
|
|
|
|
|
|
@Slf4j
|
|
|
|
|
@Component
|
|
|
|
@ -41,17 +40,17 @@ public class UserResourceCheck {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Lock4j(name = "achieveDiagnoseResourceAndOpenConnection")
|
|
|
|
|
public void achieveDiagnoseResourceAndOpenConnection(String uid, Session session) {
|
|
|
|
|
public void achieveDiagnoseResourceAndOpenConnection(String uuid, String userId, Session session) {
|
|
|
|
|
// 如果小于数字人最大连接数,则可以连接
|
|
|
|
|
if (!achieveDiagnoseResource()) {
|
|
|
|
|
try {
|
|
|
|
|
session.getBasicRemote().sendText(JSONUtil.toJsonStr(new NoResourceDTO()));
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
log.error("发送消息失败", e);
|
|
|
|
|
}
|
|
|
|
|
throw new BusinessException("暂时没有资源,建立连接失败");
|
|
|
|
|
}
|
|
|
|
|
log.info("用户:{}开始登录,缓存到Redis,并尝试把该用户已有客户端踢下线", uid);
|
|
|
|
|
// 链接之前先把之前的用户踢下线(ignoreSessionId防止把当前用户踢下线)
|
|
|
|
|
// 注意,这里如果用户没有进到问诊页面,只是在问诊大厅时,是不会被踢掉的.(因为这时没有建立websocket连接,还没与放到SESSION_POOL里面去)
|
|
|
|
|
redisTemplate.convertAndSend(UserTokenConstant.KICK_CHANNEL, JSONUtil.toJsonStr(new UserWebSocketDTO(uid, session.getId())));
|
|
|
|
|
log.info("尝试踢该用户{}的其他session下线,忽略sessionId:{}", uid, session.getId());
|
|
|
|
|
redisTemplate.opsForHash().put(UserTokenConstant.USER_WEBSOCKET_CACHE, uid, session.getId());
|
|
|
|
|
redisTemplate.opsForHash().put(UserTokenConstant.USER_WEBSOCKET_CACHE, uuid, JSONUtil.toJsonStr(new WebSocketLoginDTO(uuid, userId, session.getId())));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -59,42 +58,27 @@ public class UserResourceCheck {
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
// 实现一个方法用于踢下线用户,走的是Redis的消息队列
|
|
|
|
|
public void kickUser(String userId, String ignoreSessionId) {
|
|
|
|
|
log.info("尝试踢用户:{}下线", userId);
|
|
|
|
|
Session session = WebSocketServer.SESSION_POOL.get(userId);
|
|
|
|
|
public void kickUser(String uuid) {
|
|
|
|
|
log.info("尝试踢uuid:{}下线", uuid);
|
|
|
|
|
Session session = WebSocketSessionPool.SESSION_POOL.get(uuid);
|
|
|
|
|
// 只有不是忽略剔除的sessionId才可以踢下线
|
|
|
|
|
if (ObjectUtil.isNotEmpty(session) && !StrUtil.equals(ignoreSessionId, session.getId())) {
|
|
|
|
|
if (ObjectUtil.isNotEmpty(session)) {
|
|
|
|
|
try {
|
|
|
|
|
session.getBasicRemote().sendText(JSONUtil.toJsonStr(new Kick()));
|
|
|
|
|
session.getBasicRemote().sendText(JSONUtil.toJsonStr(new KickDTO()));
|
|
|
|
|
session.close(new CloseReason(CloseReason.CloseCodes.NORMAL_CLOSURE, "用户被踢下线"));
|
|
|
|
|
WebSocketServer.SESSION_POOL.remove(userId);
|
|
|
|
|
log.info("踢用户:{},sessionId:{} 下线成功", userId, session.getId());
|
|
|
|
|
WebSocketSessionPool.SESSION_POOL.remove(uuid);
|
|
|
|
|
// 然后从redis中移除掉
|
|
|
|
|
redisTemplate.opsForHash().delete(UserTokenConstant.USER_WEBSOCKET_CACHE, uuid);
|
|
|
|
|
log.info("踢UUID:{},sessionId:{} 下线成功", uuid, session.getId());
|
|
|
|
|
return;
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
log.error("用户:{}的websocket连接异常", e.getMessage());
|
|
|
|
|
// 连接异常的用户,移除
|
|
|
|
|
WebSocketServer.SESSION_POOL.remove(userId);
|
|
|
|
|
// 移除redis中该用户的缓存
|
|
|
|
|
redisTemplate.opsForHash().delete(UserTokenConstant.USER_WEBSOCKET_CACHE, userId);
|
|
|
|
|
}
|
|
|
|
|
// TODO 如果用户连接异常,怎么办
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
log.info("踢用户:{}下线,未找到用户,踢下线失败", userId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Data
|
|
|
|
|
public static class Kick {
|
|
|
|
|
private final Integer code = UserTokenConstant.KICK_CODE;
|
|
|
|
|
|
|
|
|
|
private final String message = "用户被踢下线";
|
|
|
|
|
|
|
|
|
|
log.info("踢UUID:{}下线,未找到用户,踢下线失败", uuid);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Data
|
|
|
|
|
public static class Keepalive {
|
|
|
|
|
private final Integer code = UserTokenConstant.KEEPALIVE_CODE;
|
|
|
|
|
|
|
|
|
|
private final String message = "keepalive";
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|