zainali5120
2020-09-21 82dfeaa3fcf871cb604e82abcc8a6db7581e83a2
src/main/java/com/xcong/excoin/websocket/TradePlateSendWebSocket.java
@@ -28,8 +28,6 @@
@ServerEndpoint(value = "/trade/market")
@Component
public class TradePlateSendWebSocket {
    @Resource
    RedisUtils redisUtils;
    /**
     * 记录当前在线连接数
@@ -155,6 +153,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);
            }
        }
        // 取消订阅
@@ -245,6 +274,7 @@
    /**
     * 发送历史K线
     *
     * @param message, toSession
     * @return void
     */
@@ -257,6 +287,7 @@
    /**
     * 发送最新K线
     *
     * @param symbol, period, message, fromSession
     * @return void
     */
@@ -273,4 +304,11 @@
            }
        }
    }
    public void sendMessageKlineNow(String message, Session toSession) {
        boolean open = toSession.isOpen();
        if (open) {
            toSession.getAsyncRemote().sendText(message);
        }
    }
}