| | |
| | | package org.example.controller; |
| | | |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.example.enums.EStockType; |
| | | import org.example.server.wcServer; |
| | | import org.java_websocket.client.WebSocketClient; |
| | | import org.springframework.context.annotation.Bean; |
| | | import org.springframework.context.annotation.Configuration; |
| | | |
| | | import java.net.URI; |
| | | import java.util.HashMap; |
| | | import java.util.Map; |
| | | |
| | | /** |
| | | * @ClassDescription: 客户端请求类 |
| | | * @JdkVersion: 1.8 |
| | | * @Created: 2023/8/31 16:13 |
| | | */ |
| | | @Slf4j |
| | | @Configuration |
| | | public class WsClientController { |
| | | |
| | | @Bean |
| | | public Map<String, WebSocketClient> websocketRunClientMap() { |
| | | |
| | | Map<String, WebSocketClient> retMap = new HashMap<>(2); |
| | | try { |
| | | wcServer websocketRunClient = new wcServer(new URI("ws://api-jp-ws.js-stock.top")); |
| | | websocketRunClient.connect(); |
| | | websocketRunClient.setConnectionLostTimeout(0); |
| | | new Thread(() -> { |
| | | while (true) { |
| | | try { |
| | | Thread.sleep(8000); |
| | | websocketRunClient.send("heartbeat".getBytes()); |
| | | } catch (Exception e) { |
| | | websocketRunClient.reconnect(); |
| | | websocketRunClient.setConnectionLostTimeout(0); |
| | | } |
| | | } |
| | | }).start(); |
| | | } catch (Exception e) { |
| | | } |
| | | return retMap; |
| | | } |
| | | |
| | | |
| | | } |
| | | |