| | |
| | | private static final String RESULT_KEY = "result"; // 定义结果键的常量 |
| | | private static final String BIDS_KEY = "bids"; // 定义 bids 的常量 |
| | | private static final String ASKS_KEY = "asks"; // 定义 asks 的常量 |
| | | private static final String p = "p"; // 定义 asks 的常量 |
| | | private static final String v = "v"; // 定义 asks 的常量 |
| | | private static final String S_KEY = "s"; // 定义 s 的常量 |
| | | |
| | | @OnMessage |
| | |
| | | Object asksObj = resultMap.get(BIDS_KEY); |
| | | Object bidsObj = resultMap.get(ASKS_KEY); |
| | | |
| | | if (asksObj instanceof List && !((List<?>) asksObj).isEmpty()) { |
| | | List<?> asksList = (List<?>) asksObj; |
| | | String[][] dataArray = gson.fromJson(gson.toJson(asksList), String[][].class); |
| | | HashMap<String, Object> pvMap = new HashMap<>(); // 创建新的 HashMap 保存 bids 和 asks |
| | | String[] asksData = dataArray[0]; |
| | | pvMap.put(p, new BigDecimal(asksData[0])); |
| | | pvMap.put(v, new BigDecimal(asksData[1])); |
| | | hashMap.put(BIDS_KEY, pvMap); // 放入 bids 数据 |
| | | } |
| | | if(asksObj instanceof List && !((List<?>) asksObj).isEmpty() && bidsObj instanceof List && !((List<?>) bidsObj).isEmpty()){ |
| | | if (asksObj instanceof List && !((List<?>) asksObj).isEmpty()) { |
| | | List<?> asksList = (List<?>) asksObj; |
| | | String[][] dataArray = gson.fromJson(gson.toJson(asksList), String[][].class); |
| | | HashMap<String, Object> pvMap = new HashMap<>(); // 创建新的 HashMap 保存 bids 和 asks |
| | | String[] asksData = dataArray[0]; |
| | | pvMap.put("p", new BigDecimal(asksData[0]).toPlainString()); |
| | | pvMap.put("v", new BigDecimal(asksData[1]).toPlainString()); |
| | | hashMap.put(BIDS_KEY, pvMap); // 放入 bids 数据 |
| | | } |
| | | |
| | | if (bidsObj instanceof List && !((List<?>) bidsObj).isEmpty()) { |
| | | List<?> bidsList = (List<?>) bidsObj; |
| | | String[][] dataArray = gson.fromJson(gson.toJson(bidsList), String[][].class); |
| | | String[] bidsData = dataArray[0]; |
| | | HashMap<String, Object> pvMap = new HashMap<>(); // 创建新的 HashMap 保存 bids 和 asks |
| | | pvMap.put(p, new BigDecimal(bidsData[0])); |
| | | pvMap.put(v, new BigDecimal(bidsData[1])); |
| | | hashMap.put(ASKS_KEY,pvMap); |
| | | } |
| | | if (bidsObj instanceof List && !((List<?>) bidsObj).isEmpty()) { |
| | | List<?> bidsList = (List<?>) bidsObj; |
| | | String[][] dataArray = gson.fromJson(gson.toJson(bidsList), String[][].class); |
| | | String[] bidsData = dataArray[0]; |
| | | HashMap<String, Object> pvMap = new HashMap<>(); // 创建新的 HashMap 保存 bids 和 asks |
| | | pvMap.put("p", new BigDecimal(bidsData[0]).toPlainString()); |
| | | pvMap.put("v", new BigDecimal(bidsData[1]).toPlainString()); |
| | | hashMap.put(ASKS_KEY,pvMap); |
| | | } |
| | | |
| | | String key = "gate" + resultMap.get(S_KEY); // 生成 Redis 键 |
| | | RedisUtil.set(key.replace("_", ""), gson.toJson(hashMap)); // 存入 Redis,使用 Gson 进行序列化 |
| | | String key = "gate" + resultMap.get(S_KEY); // 生成 Redis 键 |
| | | RedisUtil.set(key.replace("_", ""), gson.toJson(hashMap)); // 存入 Redis,使用 Gson 进行序列化 |
| | | } |
| | | } |
| | | } catch (JsonSyntaxException e) { |
| | | log.error("JSON 解析异常:" + e.getMessage(), e); // 记录 JSON 解析异常 |
| | |
| | | } |
| | | |
| | | |
| | | |
| | | @OnClose |
| | | public void onClose() { |
| | | log.info("gate ws 连接已关闭,尝试重新连接..."); // 连接关闭日志 |