1
zj
2024-08-03 388cab2e8ce85f138f4d1bc3bfbf6acd0778467f
websocketSerivce/src/main/java/org/example/websocket/server/WsServer.java
@@ -67,11 +67,7 @@
    @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);
@@ -88,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
@@ -115,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("客户段订阅消息格式错误");
        }
@@ -192,14 +194,13 @@
            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 (!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()){
@@ -226,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();