1
zj
2024-08-03 388cab2e8ce85f138f4d1bc3bfbf6acd0778467f
websocketSerivce/src/main/java/org/example/websocket/server/WsServer.java
@@ -1,17 +1,23 @@
package org.example.websocket.server;
import cn.hutool.json.JSONUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.example.pojo.ConfigCurrency;
import org.example.pojo.MarketDataOut;
import org.example.pojo.bo.WsBo;
import org.example.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
@@ -61,6 +67,7 @@
    @OnOpen
    public void onOpen(Session session) {
        this.session = session;
        int count = onlineCount.incrementAndGet();
        wsServers.add(this);
@@ -77,7 +84,11 @@
    private boolean hasReceivedSubscription(Session session) {
        WsBo wsBo = getWsBoForSession(session.getId());
        return wsBo != null; // 简化逻辑
        String s = RedisUtil.get("user_" + wsBo.getUserId());
        if(null == s || s.isEmpty() && !wsBo.getToken().equals(s)){
            closeSession(session, "用户未登录");
        }
        return wsBo != null;
    }
    @OnError
@@ -104,8 +115,10 @@
    @OnMessage
    public void onMessage(String message, Session session) throws IOException {
        try {
            WsBo bean = JSONUtil.toBean(message, WsBo.class);
            threadLocalData.put(session.getId(), bean);
            if(!message.equals("ping")){
                WsBo bean = JSONUtil.toBean(message, WsBo.class);
                threadLocalData.put(session.getId(), bean);
            }
        }catch (Exception e){
            log.error("客户段订阅消息格式错误");
        }
@@ -153,7 +166,7 @@
    private Map<Session, Long> lastMessageTimeMap = new ConcurrentHashMap<>();
    private void schedulePushMessage(Session session, String message) {
    private void schedulePushMessage(Session session, String message) throws JsonProcessingException {
        WsBo wsBo = getWsBoForSession(session.getId());
        if (wsBo != null) {
            long currentTime = System.currentTimeMillis();
@@ -171,8 +184,24 @@
        }
    }
    private static final Gson gson = new Gson();
    private String megFiltration(WsBo wsBo,String message){
    private String megFiltration(WsBo wsBo,String message) throws JsonProcessingException {
        List<MarketDataOut> redisValueMap = gson.fromJson(message, new TypeToken<List<MarketDataOut>>() {}.getType());
        String key = "config_";
        String value = RedisUtil.get(key + wsBo.getUserId());
        List<ConfigCurrency> currencies = null;
        if(null != value && !value.isEmpty()){
            ObjectMapper objectMapper = new ObjectMapper();
            currencies = objectMapper.readValue(value, new TypeReference<List<ConfigCurrency>>() {});
        }
        if (!CollectionUtils.isEmpty(currencies)) {
            Set<String> filtrationSet = currencies.stream()
                    .map(f -> f.getCurrency() + f.getBuy() + f.getSell())
                    .collect(Collectors.toSet());
            redisValueMap.removeIf(data -> filtrationSet.contains(data.getBuyAndSell()));
        }
        //查询币种
        if(null != wsBo.getCurrency()){
            redisValueMap = redisValueMap.stream()
@@ -198,11 +227,19 @@
                    .filter(data -> !list.contains(data.getBuyingPlatform()) && !list.contains(data.getSellPlatform()))
                    .collect(Collectors.toList());
        }
        //过滤数据
        if(null != wsBo.getBuyAndSell()){
            List<String> list = Arrays.asList(wsBo.getBuyAndSell().split(","));
            redisValueMap = redisValueMap.stream()
                    .filter(data -> !list.contains(data.getBuyAndSell()))
                    .collect(Collectors.toList());
        }
        //自选标记
        if(null != wsBo.getIsMarker()){
            List<String> list = Arrays.asList(wsBo.getIsMarker().split(","));
            redisValueMap.stream()
                    .filter(data -> list.contains(data.getBaseAsset()))
                    .filter(data -> list.contains(data.getBuyAndSell()))
                    .forEach(data -> data.setMarker(true));
        }
        Gson gson = new GsonBuilder().setPrettyPrinting().create();