From 28eb26d68f7a2a4e6b316c3ea9f511aa143db66e Mon Sep 17 00:00:00 2001
From: KKSU <15274802129@163.com>
Date: Mon, 30 Sep 2024 16:57:18 +0800
Subject: [PATCH] 55测试环境
---
src/main/java/com/xcong/excoin/websocket/TradePlateSendWebSocket.java | 103 +++++++++++++++++++++++++++++++++++++--------------
1 files changed, 74 insertions(+), 29 deletions(-)
diff --git a/src/main/java/com/xcong/excoin/websocket/TradePlateSendWebSocket.java b/src/main/java/com/xcong/excoin/websocket/TradePlateSendWebSocket.java
index 5eedf8e..c676e8c 100644
--- a/src/main/java/com/xcong/excoin/websocket/TradePlateSendWebSocket.java
+++ b/src/main/java/com/xcong/excoin/websocket/TradePlateSendWebSocket.java
@@ -28,8 +28,6 @@
@ServerEndpoint(value = "/trade/market")
@Component
public class TradePlateSendWebSocket {
- @Resource
- RedisUtils redisUtils;
/**
* 记录当前在线连接数
@@ -47,7 +45,7 @@
@OnOpen
public void onOpen(Session session) {
onlineCount.incrementAndGet(); // 在线数加1
- log.info("有新连接加入:{},当前在线人数为:{}", session.getId(), onlineCount.get());
+ // log.info("有新连接加入:{},当前在线人数为:{}", session.getId(), onlineCount.get());
}
/**
@@ -57,19 +55,19 @@
public void onClose(Session session) {
onlineCount.decrementAndGet(); // 在线数减1
Collection<Map<String, Session>> values = tradeplateClients.values();
- if(CollectionUtils.isNotEmpty(values)){
- for(Map<String,Session> map : values){
+ if (CollectionUtils.isNotEmpty(values)) {
+ for (Map<String, Session> map : values) {
map.remove(session.getId());
}
}
Collection<Map<String, Session>> klineClientsValues = klineClients.values();
- if(CollectionUtils.isNotEmpty(klineClientsValues)){
- for(Map<String,Session> map : klineClientsValues){
+ if (CollectionUtils.isNotEmpty(klineClientsValues)) {
+ for (Map<String, Session> map : klineClientsValues) {
map.remove(session.getId());
}
}
- log.info("有一连接关闭:{},当前在线人数为:{}", session.getId(), onlineCount.get());
+ //log.info("有一连接关闭:{},当前在线人数为:{}", session.getId(), onlineCount.get());
}
/**
@@ -81,6 +79,7 @@
public void onMessage(String message, Session session) {
// 盘口订阅方法 {sub: 'market.btcusdt.depth.10,id: symbol}
JSONObject jsonObject = JSON.parseObject(message);
+// log.info("订阅参数:{}", jsonObject);
// 盘口的判断
if (jsonObject.containsKey("sub") && jsonObject.get("sub").toString().contains("depth")) {
@@ -99,22 +98,22 @@
// 发送订阅消息
String nekk = factory.getTrader(SymbolsConstats.ROC).sendTradePlateMessage();
SubResultModel subResultModel = new SubResultModel();
- subResultModel.setId("rocusdt");
+ subResultModel.setId("griceusdt");
subResultModel.setSubbed(sub);
- synchronized (session){
+ synchronized (session) {
try {
session.getBasicRemote().sendText(JSONObject.toJSONString(subResultModel));
} catch (IOException e) {
e.printStackTrace();
}
}
- synchronized (session){
- try {
- session.getBasicRemote().sendText(nekk);
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
+ synchronized (session) {
+ try {
+ session.getBasicRemote().sendText(nekk);
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
}
@@ -140,7 +139,7 @@
String symbol = split[1];
symbol = CoinTypeConvert.convert(symbol);
String period = split[3];
- if("60min".equals(period)){
+ if ("60min".equals(period)) {
period = "1hour";
}
String key = symbol + "-" + period;
@@ -155,6 +154,37 @@
stringSessionMap.put(session.getId(), session);
klineClients.put(key, stringSessionMap);
}
+ // 给他发送最新K线 TODO
+ String newKline = "NEW_KINE_{}";
+ key = StrUtil.format(newKline, symbol);
+ RedisUtils redisUtils = SpringContextHolder.getBean(RedisUtils.class);
+ Object o = redisUtils.get(key);
+ Map<String, Candlestick> currentKlineMap = (Map<String, Candlestick>) o;
+ String ch = "market.{}.kline.{}";
+ String chKey = period;
+ if (period.equals("60min")) {
+ chKey = "1hour";
+ }
+ if(currentKlineMap!=null && currentKlineMap.containsKey(chKey)){
+ Candlestick value = currentKlineMap.get(chKey);
+ // 转换
+ NewCandlestick newCandlestick = new NewCandlestick();
+ String nekkusdt = split[1];
+ ch = StrUtil.format(ch, nekkusdt, period);
+ newCandlestick.setCh(ch);
+ CandlestickModel model = new CandlestickModel();
+ model.setVol(value.getVolume());
+ model.setLow(value.getLow());
+ model.setOpen(value.getOpen());
+ model.setHigh(value.getHigh());
+ model.setCount(value.getCount());
+ model.setAmount(value.getAmount());
+ model.setId(value.getTimestamp() / 1000);
+ model.setTimestamp(value.getTimestamp() / 1000);
+ model.setClose(value.getClose());
+ newCandlestick.setTick(model);
+ sendMessageKlineNow(JSONObject.toJSONString(newCandlestick), session);
+ }
}
// 取消订阅
@@ -163,7 +193,7 @@
String unsub = jsonObject.get("unsub").toString();
String[] split = unsub.split("\\.");
String strPeriod = split[3];
- if("60min".equals(strPeriod)){
+ if ("60min".equals(strPeriod)) {
strPeriod = "1hour";
}
String symbol = split[1];
@@ -182,7 +212,7 @@
String symbol = split[1];
symbol = CoinTypeConvert.convert(symbol);
String period = split[3];
- if("60min".equals(period)){
+ if ("60min".equals(period)) {
period = "1hour";
}
//String key = symbol+"-"+period;
@@ -190,21 +220,27 @@
String key = "KINE_{}_{}";
// 币币k线数据
//key = StrUtil.format(key, symbol, period);
- key = StrUtil.format(key, "ROC/USDT", period);
+ key = StrUtil.format(key, "GRICE/USDT", period);
RedisUtils bean = SpringContextHolder.getBean(RedisUtils.class);
Object o = bean.get(key);
List<CandlestickModel> candlestickModels = new ArrayList<>();
CandlestickResult result = new CandlestickResult();
result.setRep(sub);
- if(o!=null){
- List<Candlestick> list = (List<Candlestick>)o;
- for(Candlestick candlestick : list){
- CandlestickModel model = new CandlestickModel();
+ if (o != null) {
+ List<Candlestick> list = (List<Candlestick>) o;
+
+ if(list!=null && list.size()>300){
+ int size = list.size();
+ list = list.subList(size-300,size);
+ }
+ CandlestickModel model = null;
+ for (Candlestick candlestick : list) {
+ model = new CandlestickModel();
model.setAmount(candlestick.getAmount());
model.setClose(candlestick.getClose());
model.setCount(candlestick.getCount());
model.setHigh(candlestick.getHigh());
- model.setId(candlestick.getTimestamp()/1000);
+ model.setId(candlestick.getTimestamp() / 1000);
model.setOpen(candlestick.getOpen());
model.setLow(candlestick.getLow());
model.setVol(candlestick.getVolume());
@@ -219,8 +255,8 @@
@OnError
public void onError(Session session, Throwable error) {
- log.error("发生错误");
- error.printStackTrace();
+ // log.error("发生错误");
+ //error.printStackTrace();
}
/**
@@ -228,7 +264,7 @@
*
* @param message 消息内容
*/
- public void sendMessagePlate(String symbol,String message, Session fromSession) {
+ public void sendMessagePlate(String symbol, String message, Session fromSession) {
if (tradeplateClients.containsKey(symbol)) {
Map<String, Session> nekk = tradeplateClients.get(symbol);
for (Map.Entry<String, Session> sessionEntry : nekk.entrySet()) {
@@ -245,6 +281,7 @@
/**
* 发送历史K线
+ *
* @param message, toSession
* @return void
*/
@@ -257,6 +294,7 @@
/**
* 发送最新K线
+ *
* @param symbol, period, message, fromSession
* @return void
*/
@@ -273,4 +311,11 @@
}
}
}
+
+ public void sendMessageKlineNow(String message, Session toSession) {
+ boolean open = toSession.isOpen();
+ if (open) {
+ toSession.getAsyncRemote().sendText(message);
+ }
+ }
}
--
Gitblit v1.9.1