WebSocketServer


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

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM