| | |
| | | import javax.websocket.*; |
| | | import java.io.IOException; |
| | | import java.io.UncheckedIOException; |
| | | import java.lang.reflect.Type; |
| | | import java.math.BigDecimal; |
| | | import java.net.URI; |
| | | import java.nio.ByteBuffer; |
| | | import java.util.HashMap; |
| | |
| | | public void onMessage(String message) { |
| | | try { |
| | | Map<String, Object> map = gson.fromJson(message, new TypeToken<Map<String, Object>>() {}.getType()); |
| | | |
| | | if (map != null && map.containsKey("s")) { |
| | | Object object = map.get("d"); |
| | | Map<String, Object> resultMap = gson.fromJson(object.toString(), new TypeToken<Map<String, Object>>() {}.getType()); |
| | | HashMap<String,Object> hashMap = new HashMap<>(); |
| | | Object asksObj = resultMap.get("asks"); |
| | | Object bidsObj = resultMap.get("bids"); |
| | | |
| | | Type listType = new TypeToken<List<Map<String,Object>>>(){}.getType(); |
| | | List<Map<String,Object>> asksList = gson.fromJson(asksObj.toString(), listType); |
| | | List<Map<String,Object>> bidsList = gson.fromJson(bidsObj.toString(), listType); |
| | | |
| | | if (!asksList.isEmpty()) { |
| | | |
| | | Map<String,Object> objectMap = asksList.get(0); |
| | | HashMap<String, Object> pvMap = new HashMap<>(); // 创建新的 HashMap 保存 bids 和 asks |
| | | pvMap.put("p", new BigDecimal(objectMap.get("p").toString())); |
| | | pvMap.put("v", new BigDecimal(objectMap.get("v").toString())); |
| | | hashMap.put("asks", pvMap); |
| | | } |
| | | |
| | | if (!bidsList.isEmpty()) { |
| | | Map<String,Object> objectMap = bidsList.get(0); |
| | | HashMap<String, Object> pvMap = new HashMap<>(); // 创建新的 HashMap 保存 bids 和 asks |
| | | pvMap.put("p", new BigDecimal(objectMap.get("p").toString())); |
| | | pvMap.put("v", new BigDecimal(objectMap.get("v").toString())); |
| | | hashMap.put("bids", pvMap); |
| | | } |
| | | |
| | | ObjectMapper mapper = new ObjectMapper(); |
| | | hashMap.put("bids",resultMap.get("bids")); |
| | | hashMap.put("asks",resultMap.get("asks")); |
| | | String key = "mexc" + map.get("s").toString(); |
| | | RedisUtil.set(key, mapper.writeValueAsString(hashMap)); |
| | | try { |
| | | RedisUtil.set(key, mapper.writeValueAsString(hashMap)); |
| | | } catch (JsonProcessingException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | } catch (JsonSyntaxException e) { |
| | | log.error("JSON 解析异常:" + e.getMessage(), e); |
| | | } catch (JsonProcessingException e) { |
| | | throw new RuntimeException(e); |
| | | } |
| | | } |
| | | |
| | |
| | | |
| | | root.put("method", "SUBSCRIPTION"); |
| | | ArrayNode paramsArray = mapper.createArrayNode(); |
| | | String customParam = String.format("spot@public.limit.depth.v3.api@%s@20", symbol); |
| | | String customParam = String.format("spot@public.limit.depth.v3.api@%s@5", symbol); |
| | | paramsArray.add(customParam); |
| | | root.set("params", paramsArray); |
| | | |