Files
dating/src/main/java/com/bao/dating/session/WsSessionManager.java

52 lines
1.2 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package com.bao.dating.session;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.WebSocketSession;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
* WebSocketSession 管理类
* @author KilLze
*/
@Component
public class WsSessionManager {
private final Map<Long, WebSocketSession> SESSION_MAP = new ConcurrentHashMap<>();
/**
* 添加 WebSocketSession
* @param userId 用户ID
* @param session WebSocketSession
*/
public void addSession(Long userId, WebSocketSession session) {
SESSION_MAP.put(userId, session);
}
/**
* 移除 WebSocketSession
* @param userId 用户ID
*/
public void removeSession(Long userId) {
SESSION_MAP.remove(userId);
}
/**
* 获取 WebSocketSession
* @param userId 用户ID
* @return WebSocketSession
*/
public WebSocketSession getSession(Long userId) {
return SESSION_MAP.get(userId);
}
/**
* 判断用户是否在线
* @param userId 用户ID
* @return true-在线false-离线
*/
public boolean isOnline(Long userId) {
return SESSION_MAP.containsKey(userId);
}
}