1
zj
2024-07-29 ef299388e071ce75de6623009f21e425b5a3b017
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,11 @@
    @OnOpen
    public void onOpen(Session session) {
        WsBo wsBo = getWsBoForSession(session.getId());
        String s = RedisUtil.get("user_" + wsBo.getUserId());
        if(null == s || s.isEmpty()){
            closeSession(session, "用户未登录");
        }
        this.session = session;
        int count = onlineCount.incrementAndGet();
        wsServers.add(this);
@@ -153,7 +164,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 +182,25 @@
        }
    }
    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)){
            List<String> currency = currencies.stream().map(ConfigCurrency::getCurrency).collect(Collectors.toList());
            List<String> buy = currencies.stream().map(ConfigCurrency::getBuy).collect(Collectors.toList());
            List<String> sell = currencies.stream().map(ConfigCurrency::getSell).collect(Collectors.toList());
            redisValueMap = redisValueMap.stream()
                    .filter(data -> !currency.contains(data.getBaseAsset()) && !buy.contains(data.getBuyingPlatform()) && !sell.contains(data.getSellPlatform()))
                    .collect(Collectors.toList());
        }
        //查询币种
        if(null != wsBo.getCurrency()){
            redisValueMap = redisValueMap.stream()