package cn.edu.whu.boot.user.components;

import org.apache.shiro.session.Session;
import org.apache.shiro.session.SessionListener;
import org.springframework.stereotype.Component;

import java.util.concurrent.atomic.AtomicInteger;

@Component
public class CustomSessionListener implements SessionListener {
    private final AtomicInteger sessionCount = new AtomicInteger(0); // 用于统计在线的 session 数量

    @Override
    public void onStart(Session session) {
        sessionCount.incrementAndGet();
    }

    @Override
    public void onStop(Session session) {
        sessionCount.decrementAndGet();
    }

    @Override
    public void onExpiration(Session session) {
        sessionCount.decrementAndGet();
    }

    /**
     * 获取在线人数使用
     */
    public AtomicInteger getSessionCount() {
        return sessionCount;
    }
}