zj
2024-06-03 4afe73cb84c5a609662b8b4ee20693de9b86b9a3
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
package com.nq.ws;
 
 
import com.nq.utils.StockUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
 
import java.net.URI;
 
@Slf4j
@Configuration
public class WebSocketClientBeanConfig {
 
 
    @Bean
    public  WebsocketRunClient websocketRunClient(){
        try {
 
            WebsocketRunClient websocketRunClient = new WebsocketRunClient(new URI(StockUtil.WS_API));
            websocketRunClient.connect();
            websocketRunClient.setConnectionLostTimeout(0);
 
            new Thread(()->{
                while (true){
                    try {
                            Thread.sleep(8000);
                            websocketRunClient.send("heartbeat".getBytes());
                            log.info("发送心跳");
                    }catch (Exception e){
                        e.printStackTrace();
                        log.info("发送心跳出问题");
                        websocketRunClient.reconnect();
                        websocketRunClient.setConnectionLostTimeout(0);
                    }
                }
 
            }).start();
            return  websocketRunClient;
        }catch (Exception e){
            log.info("webSockect 链接异常");
        }
 
        return  null;
 
    }
}