1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
| @Override public void online(String sessionId, String userId, String loginType) { if (!StringUtils.hasText(sessionId) || !StringUtils.hasText(userId) || !StringUtils.hasText(loginType)){ return; }
String normalizedLoginType = normalizeLoginType(loginType); String userSessionsKey = getUserSessionsKey(userId, normalizedLoginType); String onlineUsersKey = getOnlineUsersKey(normalizedLoginType);
redisCache.setCacheMapValue(RedisKeyConstants.WS_ONLINE_SESSIONS_KEY, sessionId, buildSessionValue(userId, normalizedLoginType)); redisCache.expire(RedisKeyConstants.WS_ONLINE_SESSIONS_KEY, WebSocketConstants.ONLINE_TTL_SECONDS, TimeUnit.SECONDS); redisCache.addToSet(userSessionsKey, sessionId); redisCache.expire(userSessionsKey, WebSocketConstants.ONLINE_TTL_SECONDS, TimeUnit.SECONDS); redisCache.addToSet(onlineUsersKey, userId); redisCache.expire(onlineUsersKey, WebSocketConstants.ONLINE_TTL_SECONDS, TimeUnit.SECONDS); pushOnlineCount(normalizedLoginType); }
@Override public void heartbeat(String sessionId) { if (!StringUtils.hasText(sessionId)){ return; }
String sessionValue = redisCache.getCacheMapValue(RedisKeyConstants.WS_ONLINE_SESSIONS_KEY, sessionId); if (!StringUtils.hasText(sessionValue)){ return; }
String[] sessionParts = sessionValue.split(":", 2); if (sessionParts.length != 2 || !StringUtils.hasText(sessionParts[0]) || !StringUtils.hasText(sessionParts[1])){ return; }
String userId = sessionParts[0]; String loginType = sessionParts[1]; String userSessionsKey = getUserSessionsKey(userId, loginType); String onlineUsersKey = getOnlineUsersKey(loginType);
redisCache.expire(RedisKeyConstants.WS_ONLINE_SESSIONS_KEY, WebSocketConstants.ONLINE_TTL_SECONDS, TimeUnit.SECONDS); redisCache.expire(userSessionsKey, WebSocketConstants.ONLINE_TTL_SECONDS, TimeUnit.SECONDS); redisCache.expire(onlineUsersKey, WebSocketConstants.ONLINE_TTL_SECONDS, TimeUnit.SECONDS); }
|