1
zj
2024-08-07 f6bd6d0ad7b21ed124afca7c30c8fcea93a56319
kucoinClient/src/main/java/org/example/kucoinclient/wsClient/KucoinClient.java
@@ -6,9 +6,16 @@
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import lombok.extern.slf4j.Slf4j;
import org.example.kucoinclient.KucoinClientApplication;
import org.example.kucoinclient.comm.ApplicationContextProvider;
import org.example.kucoinclient.pojo.Currency;
import org.example.kucoinclient.util.RedisUtil;
import org.json.JSONException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.stereotype.Component;
import javax.websocket.*;
import java.io.UnsupportedEncodingException;
@@ -71,7 +78,7 @@
            }
        } catch (Exception e) {
            log.error("kucoin ws 连接过程中发生异常: ", e); // 捕获并记录异常
            log.error("kucoin ws 连接过程中发生异常:" + e.getMessage(), e); // 记录连接过程中发生的异常
        } finally {
            executorService.shutdown(); // 关闭调度线程池
        }
@@ -122,7 +129,11 @@
        }
        if (map.get("data") != null) {
            Object object = map.get("data"); // 获取数据内容
            processData(map.get("topic").toString(), object); // 处理数据
            if(null == map.get("topic")){
                System.out.println("-------------------出错了:"+map);
            }else{
                processData(map.get("topic").toString(), object); // 处理数据
            }
        }
    }
@@ -144,34 +155,34 @@
            ObjectMapper mapper = new ObjectMapper(); // 创建 ObjectMapper 实例
            // 空值检查,避免存储 null 值到 Redis
            if (resultMap.get("bids") != null) {
                Object bidsObj = resultMap.get("bids");
                if (bidsObj instanceof List && !((List<?>) bidsObj).isEmpty()) {
                    List<String> bidsList = (List<String>) bidsObj;
                    HashMap<String, Object> pvMap = new HashMap<>(); // 创建新的 HashMap 保存 bids 和 asks
                    pvMap.put("p", bidsList.get(0));
                    pvMap.put("V", bidsList.get(1));
                    hashMap.put("bids",pvMap);
                }
            }
            if (resultMap.get("asks") != null) {
            if (resultMap.get("bids") != null && resultMap.get("asks") != null) {
                Object asksObj = resultMap.get("bids");
                Object bidsObj = resultMap.get("asks");
                Object asksObj = resultMap.get("asks");
                if (asksObj instanceof List && !((List<?>) asksObj).isEmpty()) {
                    List<String> asksList = (List<String>) asksObj;
                    HashMap<String, Object> pvMap = new HashMap<>(); // 创建新的 HashMap 保存 bids 和 asks
                    pvMap.put("p", asksList.get(0));
                    pvMap.put("V", asksList.get(1));
                    hashMap.put("asks",pvMap);
                }
            }
                if(bidsObj instanceof List && !((List<?>) bidsObj).isEmpty() && asksObj instanceof List && !((List<?>) asksObj).isEmpty()){
                    if (bidsObj instanceof List && !((List<?>) bidsObj).isEmpty()) {
                        List<String> bidsList = (List<String>) bidsObj;
                        HashMap<String, Object> pvMap = new HashMap<>(); // 创建新的 HashMap 保存 bids 和 asks
                        pvMap.put("p", new BigDecimal(String.valueOf(bidsList.get(0))).toPlainString());
                        pvMap.put("v", new BigDecimal(String.valueOf(bidsList.get(1))).toPlainString());
                        hashMap.put("bids",pvMap);
                    }
            String symbol = extractSymbolFromTopic(topic); // 从 topic 提取符号
            String key = PREFIX + symbol; // 创建 Redis 缓存键
            try {
                RedisUtil.set(key, mapper.writeValueAsString(hashMap)); // 存储到 Redis
            } catch (JsonProcessingException e) {
                log.error("将数据存入 Redis 时出错: {}", e.getMessage()); // 输出数据存储错误日志
                    if (asksObj instanceof List && !((List<?>) asksObj).isEmpty()) {
                        List<String> asksList = (List<String>) asksObj;
                        HashMap<String, Object> pvMap = new HashMap<>(); // 创建新的 HashMap 保存 bids 和 asks
                        pvMap.put("p", new BigDecimal(String.valueOf(asksList.get(0))).toPlainString());
                        pvMap.put("v", new BigDecimal(String.valueOf(asksList.get(1))).toPlainString());
                        hashMap.put("asks",pvMap);
                    }
                    String symbol = extractSymbolFromTopic(topic); // 从 topic 提取符号
                    String key = PREFIX + symbol; // 创建 Redis 缓存键
                    try {
                        RedisUtil.set(key, mapper.writeValueAsString(hashMap)); // 存储到 Redis
                    } catch (JsonProcessingException e) {
                        log.error("将数据存入 Redis 时出错: {}", e.getMessage()); // 输出数据存储错误日志
                    }
                }
            }
        } else {
            log.error("topic--->存入redis失败"); // 输出处理失败日志
@@ -189,15 +200,15 @@
    }
    @OnClose
    public void onClose() {
    public void onClose() throws Exception {
        log.info("kucoin ws 连接已关闭,尝试重新连接..."); // 输出连接关闭日志
        handleConnectionClosedOrError(); // 处理连接关闭或错误
        handleConnectionClosedOrError();
    }
    @OnError
    public void onError(Throwable throwable) {
    public void onError(Throwable throwable) throws Exception {
        log.error("kucoin ws 发生错误: ", throwable); // 输出错误日志
        handleConnectionClosedOrError(); // 处理连接关闭或错误
        handleConnectionClosedOrError();
    }
    private void handleConnectionClosedOrError() {