1 files renamed
4 files modified
| File was renamed from bitgetsClient/src/main/java/org/example/bitgetsclient/WsBean/MexcWsBean.java |
| | |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.fasterxml.jackson.core.JsonProcessingException; |
| | | import com.fasterxml.jackson.databind.ObjectMapper; |
| | | import com.google.common.reflect.TypeToken; |
| | | import com.google.gson.Gson; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.apache.http.HttpEntity; |
| | | import org.apache.http.HttpResponse; |
| | | import org.apache.http.NameValuePair; |
| | | import org.apache.http.client.entity.UrlEncodedFormEntity; |
| | | import org.apache.http.client.methods.HttpPost; |
| | | import org.apache.http.impl.client.DefaultHttpClient; |
| | | import org.apache.http.message.BasicNameValuePair; |
| | | import org.apache.http.util.EntityUtils; |
| | | import org.example.bitgetsclient.pojo.Currency; |
| | | import org.example.bitgetsclient.server.impl.CurrencySerivceImpl; |
| | | import org.example.bitgetsclient.wsClient.BitgetClient; |
| | | import org.json.JSONException; |
| | | import org.json.JSONObject; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.beans.factory.annotation.Qualifier; |
| | | import org.springframework.context.annotation.Bean; |
| | |
| | | */ |
| | | @Slf4j |
| | | @Configuration |
| | | public class MexcWsBean { |
| | | public class BitgetsWsBean { |
| | | |
| | | @Autowired |
| | | private CurrencySerivceImpl currencyService; |
| | |
| | | symbolList.forEach(f->{ |
| | | Map<String, String> argsMap = new HashMap<>(); |
| | | argsMap.put("instType", "SPOT"); |
| | | argsMap.put("channel", "books15"); |
| | | argsMap.put("channel", "books1"); |
| | | argsMap.put("instId", f); |
| | | mapList.add(argsMap); |
| | | }); |
| | |
| | | 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 内容 |
| | |
| | | |
| | | import javax.websocket.*; |
| | | import java.io.UnsupportedEncodingException; |
| | | import java.math.BigDecimal; |
| | | import java.net.URI; |
| | | import java.net.URLEncoder; |
| | | import java.nio.ByteBuffer; |
| | |
| | | |
| | | // 空值检查,避免存储 null 值到 Redis |
| | | if (resultMap.get("bids") != null) { |
| | | hashMap.put("bids", resultMap.get("bids")); // 存储 bids |
| | | 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) { |
| | | hashMap.put("asks", resultMap.get("asks")); // 存储 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); |
| | | } |
| | | } |
| | | |
| | | String symbol = extractSymbolFromTopic(topic); // 从 topic 提取符号 |
| | |
| | | Map<String, Object> jsonMap = new HashMap<>(); |
| | | jsonMap.put("id", id); // 会话 ID |
| | | jsonMap.put("type", "subscribe"); // 订阅类型 |
| | | jsonMap.put("topic", "/spotMarket/level2Depth50:" + symbol); // 订阅的主题 |
| | | jsonMap.put("topic", "/spotMarket/level1:" + symbol); // 订阅的主题 |
| | | jsonMap.put("privateChannel", false); // 是否私有通道 |
| | | jsonMap.put("response", true); // 是否返回响应 |
| | | |
| | |
| | | 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); |
| | | |
| | |
| | | * 获取IP地址: |
| | | */ |
| | | public static String getIpAddress(HttpServletRequest request) { |
| | | try { |
| | | Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); |
| | | while (interfaces.hasMoreElements()) { |
| | | NetworkInterface networkInterface = interfaces.nextElement(); |
| | | Enumeration<InetAddress> addresses = networkInterface.getInetAddresses(); |
| | | while (addresses.hasMoreElements()) { |
| | | InetAddress address = addresses.nextElement(); |
| | | if (!address.isSiteLocalAddress() && !address.isLoopbackAddress() && address instanceof Inet4Address) { |
| | | System.out.println("External IP: " + address.getHostAddress()); |
| | | return address.getHostAddress(); |
| | | } |
| | | } |
| | | } |
| | | } catch (SocketException e) { |
| | | e.printStackTrace(); |
| | | // 获取客户端真实 IP 地址 |
| | | String ipAddress = request.getHeader("X-Forwarded-For"); |
| | | |
| | | if (ipAddress == null || ipAddress.isEmpty() || "unknown".equalsIgnoreCase(ipAddress)) { |
| | | ipAddress = request.getHeader("Proxy-Client-IP"); |
| | | } |
| | | return null; |
| | | |
| | | if (ipAddress == null || ipAddress.isEmpty() || "unknown".equalsIgnoreCase(ipAddress)) { |
| | | ipAddress = request.getHeader("WL-Proxy-Client-IP"); |
| | | } |
| | | |
| | | if (ipAddress == null || ipAddress.isEmpty() || "unknown".equalsIgnoreCase(ipAddress)) { |
| | | ipAddress = request.getRemoteAddr(); |
| | | } |
| | | |
| | | return ipAddress; |
| | | } |
| | | |
| | | /** |