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;
|
|
}
|
}
|