All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.healthy.common.websocket.session.DefaultWebSocketSessionStore Maven / Gradle / Ivy

There is a newer version: 2.0.0-M1
Show newest version
package com.healthy.common.websocket.session;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.socket.WebSocketSession;

import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

/**
 * 默认的 WebSocketSession 存储器
 *
 * @author xm.z
 */
@Slf4j
public class DefaultWebSocketSessionStore implements WebSocketSessionStore {

	private final SessionKeyGenerator sessionKeyGenerator;

	private final ConcurrentHashMap> sessionKeyToWsSessions = new ConcurrentHashMap<>();

	public DefaultWebSocketSessionStore(SessionKeyGenerator sessionKeyGenerator) {
		this.sessionKeyGenerator = sessionKeyGenerator;
	}

	/**
	 * 添加一个 wsSession
	 * @param wsSession 待添加的 WebSocketSession
	 */
	@Override
	public void addSession(WebSocketSession wsSession) {
		Object sessionKey = sessionKeyGenerator.sessionKey(wsSession);
		Map sessions = this.sessionKeyToWsSessions.get(sessionKey);
		if (sessions == null) {
			sessions = new ConcurrentHashMap<>();
			this.sessionKeyToWsSessions.putIfAbsent(sessionKey, sessions);
			sessions = this.sessionKeyToWsSessions.get(sessionKey);
		}
		sessions.put(wsSession.getId(), wsSession);
	}

	/**
	 * 删除一个 session
	 * @param session WebSocketSession
	 */
	@Override
	public void removeSession(WebSocketSession session) {
		Object sessionKey = sessionKeyGenerator.sessionKey(session);
		String wsSessionId = session.getId();

		Map sessions = this.sessionKeyToWsSessions.get(sessionKey);
		if (sessions != null) {
			boolean result = sessions.remove(wsSessionId) != null;
			if (log.isDebugEnabled()) {
				log.debug("Removal of " + wsSessionId + " was " + result);
			}
			if (sessions.isEmpty()) {
				this.sessionKeyToWsSessions.remove(sessionKey);
				if (log.isDebugEnabled()) {
					log.debug("Removed the corresponding HTTP Session for " + wsSessionId
							+ " since it contained no WebSocket mappings");
				}
			}
		}
	}

	/**
	 * 获取当前所有在线的 session
	 * @return session集合
	 */
	@Override
	public Collection getSessions() {
		return sessionKeyToWsSessions.values().stream().flatMap(x -> x.values().stream()).collect(Collectors.toList());
	}

	/**
	 * 根据指定的 sessionKey 获取对应的 wsSessions
	 * @param sessionKey wsSession 标识
	 * @return session集合
	 */
	@Override
	public Collection getSessions(Object sessionKey) {
		Map sessions = this.sessionKeyToWsSessions.get(sessionKey);
		if (sessions != null) {
			return this.sessionKeyToWsSessions.get(sessionKey).values();
		}
		log.warn("根据指定的sessionKey: {} 获取对应的wsSessions为空!", sessionKey);
		return Collections.emptyList();
	}

	/**
	 * 获取所有的 sessionKeys
	 * @return sessionKey 集合
	 */
	@Override
	public Collection getSessionKeys() {
		return sessionKeyToWsSessions.keySet();
	}

}