@Component @ServerEndpoint(value = "/endpoint/ws") public class WebSocketServer { private final Logger logger = LoggerFactory.getLogger(getClass()); private static final AtomicInteger onlineCount = new AtomicInteger(); @OnOpen public void onOpen(Session session, @PathParam("sid") String sid) { //在线数加1 onlineCount.incrementAndGet(); logger.info("新窗口监听 {}, 当前在线人数 {}",sid,onlineCount.get()); } @OnClose public void onClose() { //在线数减1 onlineCount.decrementAndGet(); logger.info("连接关闭, 当前在线人数为 {}",onlineCount.get()); } @OnError public void onError(Session session, Throwable error) { logger.warn("发生错误, 非正常关闭 {}, 当前在线人数为 {}", onlineCount.get()); } }