From 378b31595f7eebdf46149fa2052cec41f7ce9565 Mon Sep 17 00:00:00 2001
From: zj <1772600164@qq.com>
Date: Sat, 20 Jul 2024 18:06:25 +0800
Subject: [PATCH] 1

---
 websocketClient/src/main/java/org/example/WsBean/MexcWsBean.java |   79 +++++++++++++++++++++++++++++++++++++--
 1 files changed, 75 insertions(+), 4 deletions(-)

diff --git a/websocketClient/src/main/java/org/example/WsBean/MexcWsBean.java b/websocketClient/src/main/java/org/example/WsBean/MexcWsBean.java
index 0315b55..29aaf1c 100644
--- a/websocketClient/src/main/java/org/example/WsBean/MexcWsBean.java
+++ b/websocketClient/src/main/java/org/example/WsBean/MexcWsBean.java
@@ -1,17 +1,32 @@
 package org.example.WsBean;
 
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
+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.pojo.Currency;
 import org.example.server.impl.CurrencySerivceImpl;
+import org.example.wsClient.GateClient;
+import org.example.wsClient.KucoinClient;
 import org.example.wsClient.MexcClient;
+import org.json.JSONObject;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
 import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
 import org.springframework.util.CollectionUtils;
 
+import java.util.ArrayList;
 import java.util.List;
+import java.util.Map;
 
 /**
  * @ClassDescription: 客户端请求类
@@ -28,11 +43,54 @@
     @Autowired
     private ThreadPoolTaskExecutor threadPoolTaskExecutor;
 
+//    @Bean
+//    public void mexcWebsocketRunClientMap() {
+//        List<Currency> mexc = currencyService.list(new LambdaQueryWrapper<Currency>().eq(Currency::getSource, "mexc"));
+//        if (!CollectionUtils.isEmpty(mexc)) {
+//            int batchSize = 30; // 每个线程处理的数据量
+//            int totalSize = mexc.size();
+//            int threadCount = (int) Math.ceil((double) totalSize / batchSize); // 计算需要的线程数
+//
+//            for (int i = 0; i < threadCount; i++) {
+//                int fromIndex = i * batchSize;
+//                int toIndex = Math.min(fromIndex + batchSize, totalSize);
+//                List<Currency> sublist = mexc.subList(fromIndex, toIndex);
+//
+//                // 使用自定义线程池提交任务
+//                threadPoolTaskExecutor.execute(new MexcClient(sublist)::start);
+//            }
+//
+//        }
+//    }
+
+//    @Bean
+//    public void gateWebsocketRunClientMap() {
+//        List<Currency> mexc = currencyService.list(new LambdaQueryWrapper<Currency>().eq(Currency::getSource, "gate"));
+//        if (!CollectionUtils.isEmpty(mexc)) {
+//            int batchSize = 100; // 每个线程处理的数据量
+//            int totalSize = mexc.size();
+//            int threadCount = (int) Math.ceil((double) totalSize / batchSize); // 计算需要的线程数
+//
+//            for (int i = 0; i < threadCount; i++) {
+//                int fromIndex = i * batchSize;
+//                int toIndex = Math.min(fromIndex + batchSize, totalSize);
+//                List<Currency> sublist = mexc.subList(fromIndex, toIndex);
+//
+//                // 使用自定义线程池提交任务
+//                threadPoolTaskExecutor.execute(new GateClient(sublist)::start);
+//            }
+//
+//        }
+//    }
+
     @Bean
-    public void mexcWebsocketRunClientMap() {
-        List<Currency> mexc = currencyService.list(new LambdaQueryWrapper<Currency>().eq(Currency::getSource, "mexc"));
+    public void kucoinWebsocketRunClientMap() throws Exception {
+        List<Currency> mexc = currencyService.list(new LambdaQueryWrapper<Currency>().eq(Currency::getSource, "kucoin"));
         if (!CollectionUtils.isEmpty(mexc)) {
-            int batchSize = 30; // 每个线程处理的数据量
+            String result = doPost();
+            JSONObject jsonObject = new JSONObject(result);
+            String token = jsonObject.getJSONObject("data").getString("token");
+            int batchSize = 100; // 每个线程处理的数据量
             int totalSize = mexc.size();
             int threadCount = (int) Math.ceil((double) totalSize / batchSize); // 计算需要的线程数
 
@@ -42,10 +100,23 @@
                 List<Currency> sublist = mexc.subList(fromIndex, toIndex);
 
                 // 使用自定义线程池提交任务
-                threadPoolTaskExecutor.execute(new MexcClient(sublist)::start);
+                threadPoolTaskExecutor.execute(new KucoinClient(sublist,token)::start);
             }
 
         }
     }
+
+    public static String doPost() throws Exception {
+        String url = "https://api.kucoin.com/api/v1/bullet-public";
+        HttpPost httpPost = new HttpPost(url);
+        DefaultHttpClient defaultHttpClient = new DefaultHttpClient();
+        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
+        httpPost.setEntity(new UrlEncodedFormEntity(nvps));
+        HttpResponse response = defaultHttpClient.execute(httpPost);
+        HttpEntity respEntity = response.getEntity();
+        String text = EntityUtils.toString(respEntity, "UTF-8");
+        defaultHttpClient.getConnectionManager().shutdown();
+        return text;
+    }
 }
 

--
Gitblit v1.9.3