| | |
| | | import com.google.gson.Gson; |
| | | import com.google.gson.JsonSyntaxException; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.example.mexcclient.MexcClientApplication; |
| | | import org.example.mexcclient.comm.ApplicationContextProvider; |
| | | import org.example.mexcclient.pojo.Currency; |
| | | import org.example.mexcclient.util.RedisUtil; |
| | | 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.IOException; |
| | |
| | | |
| | | @ClientEndpoint |
| | | @Slf4j |
| | | @Component |
| | | public class MexcClient { |
| | | private static final String WS_ENDPOINT = "wss://wbs.mexc.com/ws"; |
| | | private static final long PING_INTERVAL = 20000; |
| | |
| | | } |
| | | |
| | | } catch (Exception e) { |
| | | log.error("mexc ws 连接过程中发生异常: " + e.getMessage(), e); |
| | | log.error("mexc ws 连接过程中发生异常:" + e.getMessage(), e); // 记录连接过程中发生的异常 |
| | | } finally { |
| | | executorService.shutdown(); |
| | | } |
| | |
| | | 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"); |
| | | Object bidsObj = resultMap.get("asks"); |
| | | Object asksObj = 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(!asksList.isEmpty() && !asksList.get(0).isEmpty() && !bidsList.isEmpty() && !bidsList.get(0).isEmpty()){ |
| | | if (!asksList.isEmpty() && !asksList.get(0).isEmpty()) { |
| | | |
| | | 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); |
| | | } |
| | | 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()).toPlainString()); |
| | | pvMap.put("v", new BigDecimal(objectMap.get("v").toString()).toPlainString()); |
| | | hashMap.put("asks", pvMap); |
| | | } |
| | | |
| | | ObjectMapper mapper = new ObjectMapper(); |
| | | String key = "mexc" + map.get("s").toString(); |
| | | try { |
| | | RedisUtil.set(key, mapper.writeValueAsString(hashMap)); |
| | | } catch (JsonProcessingException e) { |
| | | e.printStackTrace(); |
| | | if (!bidsList.isEmpty() && !bidsList.get(0).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()).toPlainString()); |
| | | pvMap.put("v", new BigDecimal(objectMap.get("v").toString()).toPlainString()); |
| | | hashMap.put("bids", pvMap); |
| | | } |
| | | |
| | | ObjectMapper mapper = new ObjectMapper(); |
| | | String key = "mexc" + map.get("s").toString(); |
| | | try { |
| | | RedisUtil.set(key, mapper.writeValueAsString(hashMap)); |
| | | } catch (JsonProcessingException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | } |
| | | } catch (JsonSyntaxException e) { |
| | |
| | | } |
| | | } |
| | | |
| | | |
| | | @OnClose |
| | | public void onClose() { |
| | | public void onClose() throws Exception { |
| | | log.info("mexc ws 连接已关闭,尝试重新连接..."); |
| | | handleConnectionClosedOrError(); |
| | | } |
| | | |
| | | @OnError |
| | | public void onError(Throwable throwable) { |
| | | public void onError(Throwable throwable) throws Exception { |
| | | log.error("mexc ws 发生错误: " + throwable.getMessage(), throwable); |
| | | handleConnectionClosedOrError(); |
| | | } |