| | |
| | | import org.json.JSONException; |
| | | |
| | | import javax.websocket.*; |
| | | import java.math.BigDecimal; |
| | | import java.net.URI; |
| | | import java.nio.ByteBuffer; |
| | | import java.util.Arrays; |
| | |
| | | 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 |
| | |
| | | if (resultMap != null && resultMap.get(S_KEY) != null) { |
| | | HashMap<String, Object> hashMap = new HashMap<>(); // 创建新的 HashMap 保存 bids 和 asks |
| | | |
| | | // 放入 bids 和 asks 数据 |
| | | hashMap.put(BIDS_KEY, resultMap.get(BIDS_KEY)); // 放入 bids 数据 |
| | | hashMap.put(ASKS_KEY, resultMap.get(ASKS_KEY)); // 放入 asks 数据 |
| | | 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 (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); |
| | | } |
| | | |
| | | String key = "gate" + resultMap.get(S_KEY); // 生成 Redis 键 |
| | | RedisUtil.set(key.replace("_", ""), gson.toJson(hashMap)); // 存入 Redis,使用 Gson 进行序列化 |
| | |
| | | final String CHANNEL = "spot.order_book"; // 固定频道名称 |
| | | final String EVENT_SUBSCRIBE = "subscribe"; // 订阅事件 |
| | | final String EVENT_UNSUBSCRIBE = "unsubscribe"; // 取消订阅事件 |
| | | final String[] PAYLOAD = new String[]{symbol, "20", "100ms"}; // 请求负载信息 |
| | | final String[] PAYLOAD = new String[]{symbol, "5", "100ms"}; // 请求负载信息 |
| | | |
| | | // 使用Map构建JSON对象 |
| | | Map<String, Object> jsonMap = new HashMap<>(); // 创建 Map 存放 JSON 内容 |