aboutsummaryrefslogtreecommitdiff
path: root/juick-server/src/test/java/com/juick/server/tests/WebsocketClientConfig.java
blob: 6be8e3c6464cb87c480aa5eea53f3eb5d6fc90ef (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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;
    }
}