@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()); } }