|
|
|
@ -13,10 +13,14 @@ import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
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
|
|
|
|
@ -50,17 +54,48 @@ public class UserResourceCheck {
|
|
|
|
|
redisTemplate.opsForHash().put(UserTokenConstant.USER_WEBSOCKET_CACHE, uid, session.getId());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* websocket保活接口
|
|
|
|
|
*/
|
|
|
|
|
@Scheduled(fixedDelay = 5 * 1000L)
|
|
|
|
|
public void keepalive() {
|
|
|
|
|
for (Map.Entry<String, Session> entries : WebSocketServer.SESSION_POOL.entrySet()) {
|
|
|
|
|
String userId = entries.getKey();
|
|
|
|
|
Session session = entries.getValue();
|
|
|
|
|
if (ObjectUtil.isNotEmpty(session)) {
|
|
|
|
|
try {
|
|
|
|
|
session.getBasicRemote().sendText(JSONUtil.toJsonStr(new Keepalive()));
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
log.error("用户:{}的websocket连接异常", e.getMessage());
|
|
|
|
|
// 连接异常的用户,移除
|
|
|
|
|
WebSocketServer.SESSION_POOL.remove(userId);
|
|
|
|
|
// 移除redis中该用户的缓存
|
|
|
|
|
redisTemplate.opsForHash().delete(UserTokenConstant.USER_WEBSOCKET_CACHE, userId);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 实现一个方法用于踢下线用户,走的是Redis的消息队列
|
|
|
|
|
public void kickUser(String userId, String ignoreSessionId) throws IOException {
|
|
|
|
|
public void kickUser(String userId, String ignoreSessionId) {
|
|
|
|
|
log.info("尝试踢用户:{}下线", userId);
|
|
|
|
|
Session session = WebSocketServer.SESSION_POOL.get(userId);
|
|
|
|
|
// 只有不是忽略剔除的sessionId才可以踢下线
|
|
|
|
|
if (ObjectUtil.isNotEmpty(session) && !StrUtil.equals(ignoreSessionId, session.getId())) {
|
|
|
|
|
try {
|
|
|
|
|
session.getBasicRemote().sendText(JSONUtil.toJsonStr(new Kick()));
|
|
|
|
|
session.close(new CloseReason(CloseReason.CloseCodes.NORMAL_CLOSURE, "用户被踢下线"));
|
|
|
|
|
WebSocketServer.SESSION_POOL.remove(userId);
|
|
|
|
|
log.info("踢用户:{},sessionId:{} 下线成功", userId, 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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
log.info("踢用户:{}下线,未找到用户,踢下线失败", userId);
|
|
|
|
|
}
|
|
|
|
@ -73,4 +108,12 @@ public class UserResourceCheck {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Data
|
|
|
|
|
public static class Keepalive {
|
|
|
|
|
private final Integer code = UserTokenConstant.KEEPALIVE_CODE;
|
|
|
|
|
|
|
|
|
|
private final String message = "keepalive";
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|