package com.juick.server.tests; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory; import org.springframework.boot.web.servlet.server.ServletWebServerFactory; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.socket.client.WebSocketConnectionManager; import org.springframework.web.socket.client.standard.StandardWebSocketClient; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; @Configuration public class WebsocketClientConfig implements CommandLineRunner { private static Logger logger = LoggerFactory.getLogger(ServerTests.class); @Bean public WebsocketClientHandler handler() { return new WebsocketClientHandler(latch); } @Bean ServletWebServerFactory servletWebServerFactory(){ return new TomcatServletWebServerFactory(); } @Bean public WebSocketConnectionManager connectionManager() { WebSocketConnectionManager manager = new WebSocketConnectionManager(client(), handler(), "ws://localhost:8080/ws/"); manager.setAutoStartup(true); return manager; } @Bean public StandardWebSocketClient client() { return new StandardWebSocketClient(); } private final CountDownLatch latch = new CountDownLatch(1); @Override public void run(String... args) throws Exception { if (latch.await(10, TimeUnit.SECONDS)) { logger.info("client connected"); } } public CountDownLatch getLatch() { return latch; } }