WebSocketConfig { public <T, V> void listen(String path, Class<T> clientMessageClass, Class<V> serverMessageClass, ChannelListener<T, V> listener) { if (HTTPIOHandler.HEALTH_CHECK_PATH.equals(path)) throw new Error("/health-check is reserved path"); if (path.contains("/:")) throw new Error("listener path must be static, path=" + path); if (listener.getClass().isSynthetic()) throw new Error("listener class must not be anonymous class or lambda, please create static class, listenerClass=" + listener.getClass().getCanonicalName()); new InjectValidator(listener).validate(); logger.info("ws, path={}, clientMessageClass={}, serverMessageClass={}, listener={}", path, clientMessageClass.getCanonicalName(), serverMessageClass.getCanonicalName(), listener.getClass().getCanonicalName()); if (context.httpServer.handler.webSocketHandler == null) { context.httpServer.handler.webSocketHandler = new WebSocketHandler(context.logManager, context.httpServer.siteManager.sessionManager, context.httpServer.handler.rateControl); context.beanFactory.bind(WebSocketContext.class, null, context.httpServer.handler.webSocketHandler.context); } context.beanClassValidator.validate(clientMessageClass); context.beanClassValidator.validate(serverMessageClass); context.serviceRegistry.beanClasses.add(clientMessageClass); context.serviceRegistry.beanClasses.add(serverMessageClass); var handler = new ChannelHandler<>(clientMessageClass, serverMessageClass, listener); context.httpServer.handler.webSocketHandler.add(path, handler); } WebSocketConfig(ModuleContext context); void listen(String path, Class<T> clientMessageClass, Class<V> serverMessageClass, ChannelListener<T, V> listener);  }
@Test void withReservedPath() { assertThatThrownBy(() -> config.listen(HTTPIOHandler.HEALTH_CHECK_PATH, TestWebSocketMessage.class, TestWebSocketMessage.class, new TestChannelListener())) .isInstanceOf(Error.class) .hasMessageContaining("/health-check is reserved path"); }
@Test void listen() { assertThatThrownBy(() -> config.listen("/ws/:name", null, null, null)) .isInstanceOf(Error.class) .hasMessageContaining("listener path must be static"); assertThatThrownBy(() -> config.listen("/ws", TestWebSocketMessage.class, TestWebSocketMessage.class, (channel, message) -> { })).isInstanceOf(Error.class) .hasMessageContaining("listener class must not be anonymous class or lambda"); config.listen("/ws2", TestWebSocketMessage.class, TestWebSocketMessage.class, new TestChannelListener()); var webSocketContext = (WebSocketContext) config.context.beanFactory.bean(WebSocketContext.class, null); assertThat(webSocketContext).isNotNull(); assertThat(config.context.serviceRegistry.beanClasses).contains(TestWebSocketMessage.class); }