Преглед изворни кода

判断用户是否在线。

wukai пре 2 година
родитељ
комит
344c91a887

+ 21 - 3
doc-biz/src/main/java/com/doc/biz/ws/WebSocketServer.java

@@ -5,6 +5,7 @@ import com.doc.chat.domain.ChatMsg;
 import com.doc.chat.service.IChatMsgService;
 import com.doc.chat.service.impl.ChatMsgServiceImpl;
 import com.doc.common.constant.CacheConstants;
+import com.doc.common.core.redis.RedisCache;
 import com.doc.common.utils.spring.SpringUtils;
 import com.fasterxml.jackson.databind.ObjectMapper;
 import lombok.extern.slf4j.Slf4j;
@@ -19,6 +20,7 @@ import javax.websocket.Session;
 import javax.websocket.server.PathParam;
 import javax.websocket.server.ServerEndpoint;
 import java.io.IOException;
+import java.util.ArrayList;
 import java.util.List;
 import java.util.concurrent.ConcurrentHashMap;
 
@@ -33,6 +35,8 @@ import java.util.concurrent.ConcurrentHashMap;
 public class WebSocketServer {
     private IChatMsgService msgService = SpringUtils.getBean(ChatMsgServiceImpl.class);
     private RedisTemplate redisTemplate = SpringUtils.getBean(StringRedisTemplate.class);
+
+    private RedisCache redisCache = SpringUtils.getBean(RedisCache.class);
     /**
      * 保存 组id->组成员 的映射关系
      * 之所以使用ConcurrentHashMap因为这个是线程安全的
@@ -47,6 +51,7 @@ public class WebSocketServer {
         //需要通知其他的客户端,将所有的用户的用户名发送给客户端
         //将当前连接的session存储到redis中
         currentMap.put(uid, session);
+        setOnlineUserListCache();
         log.info("用户(ID:{})建立连接!", uid);
         try {
             session.getBasicRemote().sendText("连接成功");
@@ -94,7 +99,8 @@ public class WebSocketServer {
                 this.sendClusterMessage(message);
             }
         } catch (Exception e) {
-            e.printStackTrace();
+            log.error("会话异常!" + e.getMessage());
+//            e.printStackTrace();
         }
     }
 
@@ -105,6 +111,18 @@ public class WebSocketServer {
     public void onClose(@PathParam("uid") Long uid, Session session) {
         log.info("用户(ID:{})关闭连接!", uid);
         currentMap.remove(uid);
+        setOnlineUserListCache();
+    }
+
+    /**
+     * 设置在线用户列表
+     */
+    private void setOnlineUserListCache() {
+        List<Long> onlineUsers = new ArrayList<>(currentMap.keySet());
+        redisCache.deleteObject(CacheConstants.ONLINE_USERS);
+        if (onlineUsers.size() > 0) {
+            redisCache.setCacheList(CacheConstants.ONLINE_USERS, onlineUsers);
+        }
     }
 
     /**
@@ -126,8 +144,8 @@ public class WebSocketServer {
                     session.getBasicRemote().sendText(JSON.toJSONString(msg));
                 }
             } catch (Exception e) {
-                log.info("会话异常!" + e.getMessage());
-                e.printStackTrace();
+                log.error("会话异常!" + e.getMessage());
+//                e.printStackTrace();
             }
         }
     }

+ 6 - 3
doc-common/src/main/java/com/doc/common/constant/CacheConstants.java

@@ -2,15 +2,18 @@ package com.doc.common.constant;
 
 /**
  * 缓存的key 常量
- * 
+ *
  * @author ruoyi
  */
-public class CacheConstants
-{
+public class CacheConstants {
     /**
      * 登录用户 redis key
      */
     public static final String LOGIN_TOKEN_KEY = "login_tokens:";
+    /**
+     * 登录用户ID列表 redis key
+     */
+    public static final String ONLINE_USERS = "online_users";
 
     /**
      * 验证码 redis key

+ 10 - 0
doc-system/src/main/java/com/doc/system/service/ISysUserService.java

@@ -1,6 +1,7 @@
 package com.doc.system.service;
 
 import com.doc.common.core.domain.entity.SysUser;
+import javafx.util.Pair;
 
 import java.util.List;
 import java.util.Map;
@@ -214,8 +215,17 @@ public interface ISysUserService {
 
     /**
      * 根据部门ID查询用户列表
+     *
      * @param deptId 部门ID
      * @return
      */
     List<SysUser> selectUserListByDeptId(Long deptId);
+
+    /**
+     * 判断用户websocket是否在线,并返回手机号码
+     *
+     * @param uid 用户ID
+     * @return 是否在线
+     */
+    Pair<Boolean, String> isOnline(Long uid);
 }

+ 17 - 0
doc-system/src/main/java/com/doc/system/service/impl/SysUserServiceImpl.java

@@ -11,6 +11,7 @@ import com.doc.common.exception.ServiceException;
 import com.doc.common.utils.SecurityUtils;
 import com.doc.common.utils.StringUtils;
 import com.doc.common.utils.bean.BeanValidators;
+import com.doc.common.utils.encrypt.Sm2Util;
 import com.doc.common.utils.spring.SpringUtils;
 import com.doc.system.domain.SysPost;
 import com.doc.system.domain.SysUserPost;
@@ -19,6 +20,7 @@ import com.doc.system.mapper.*;
 import com.doc.system.service.ISysConfigService;
 import com.doc.system.service.ISysUserExpandService;
 import com.doc.system.service.ISysUserService;
+import javafx.util.Pair;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.stereotype.Service;
@@ -549,4 +551,19 @@ public class SysUserServiceImpl implements ISysUserService {
         queryWrapper.ne("user_id", "1");
         return userMapper.selectList(queryWrapper);
     }
+
+    /**
+     * 判断用户websocket是否在线
+     *
+     * @param uid 用户ID
+     * @return 是否在线
+     */
+    @Override
+    public Pair<Boolean, String> isOnline(Long uid) {
+        List<Long> onlineUsers = redisCache.getCacheList(CacheConstants.ONLINE_USERS);
+        boolean isOnline = onlineUsers.contains(uid);
+        SysUser user = selectUserById(uid);
+        String phone = Sm2Util.decrypt(user.getPhonenumber());
+        return new Pair<>(isOnline, phone);
+    }
 }