1、pom文件
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
2、工具类
@Slf4j
@Component
public class WebSocketSessionUtil {
@Resource
private RedisUtil redisUtil;
public static final String CONNECT_ID = "connectId";
public static final String USER_ID = "userId";
public static final String SNOW_FLAKE_ID = "snowFlakeId";
private static Map<String, WebSocketSession> SESSION_MAP = new ConcurrentHashMap<>();
public void addUserSeesion(String userId, String snowflakeId, WebSocketSession webSocketSession) {
SESSION_MAP.put(snowflakeId, webSocketSession);
boolean bol = redisUtil.lSet(RedisKey.WEBSOCKET_USER + userId, snowflakeId);
if (bol) {
log.debug("用户【 {} 】连接 snowflakeId: {}", userId, snowflakeId);
}
}
public void removeUserSeesion(String userId, String snowflakeId) {
SESSION_MAP.remove(snowflakeId);
long size = redisUtil.lRemove(RedisKey.WEBSOCKET_USER + userId, 0, snowflakeId);
if (size > 0) {
log.debug("用户【 {} 】退出 snowflakeId: {}", userId<