From 388cab2e8ce85f138f4d1bc3bfbf6acd0778467f Mon Sep 17 00:00:00 2001
From: zj <1772600164@qq.com>
Date: Sat, 03 Aug 2024 23:34:52 +0800
Subject: [PATCH] 1
---
websocketSerivce/src/main/java/org/example/websocket/server/WsServer.java | 49 +++++++++++++++++++++++++++++++++++++++++++------
1 files changed, 43 insertions(+), 6 deletions(-)
diff --git a/websocketSerivce/src/main/java/org/example/websocket/server/WsServer.java b/websocketSerivce/src/main/java/org/example/websocket/server/WsServer.java
index 530db1e..9f594a1 100644
--- a/websocketSerivce/src/main/java/org/example/websocket/server/WsServer.java
+++ b/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();
--
Gitblit v1.9.3