| | |
| | | package org.example.websocket.server; |
| | | |
| | | import cn.hutool.core.util.NumberUtil; |
| | | 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.fasterxml.jackson.databind.module.SimpleModule; |
| | | import com.google.common.reflect.TypeToken; |
| | | import com.google.gson.Gson; |
| | | import com.google.gson.GsonBuilder; |
| | |
| | | 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; |
| | | |
| | | import javax.annotation.PostConstruct; |
| | | import javax.websocket.*; |
| | | import javax.websocket.server.ServerEndpoint; |
| | | import java.io.IOException; |
| | | import java.lang.reflect.Type; |
| | | import java.math.BigDecimal; |
| | | import java.nio.ByteBuffer; |
| | | import java.text.SimpleDateFormat; |
| | | import java.util.*; |
| | | import java.util.concurrent.*; |
| | | import java.util.concurrent.atomic.AtomicBoolean; |
| | | import java.util.concurrent.atomic.AtomicInteger; |
| | | import java.util.concurrent.locks.Lock; |
| | | import java.util.concurrent.locks.ReentrantLock; |
| | | import java.util.stream.Collectors; |
| | | import java.util.zip.Deflater; |
| | | import java.util.zip.DeflaterOutputStream; |
| | | |
| | | /** |
| | | * @ClassDescription: websocket服务端 |
| | |
| | | |
| | | @OnError |
| | | public void onError(Session session, @NonNull Throwable throwable) { |
| | | threadLocalData.remove(session.getId()); |
| | | wsServers.remove(this); |
| | | onClose(); |
| | | log.error("连接发生报错: {}", throwable.getMessage()); |
| | | throwable.printStackTrace(); |
| | | } |
| | |
| | | if(!message.equals("ping")){ |
| | | WsBo bean = JSONUtil.toBean(message, WsBo.class); |
| | | if(null == bean){ |
| | | closeSession(session,"没有订阅消息"); |
| | | log.error("没有订阅消息"); |
| | | closeSession(session,null); |
| | | } |
| | | String s = RedisUtil.get(bean.getUserId().toString()); |
| | | if(StringUtils.isEmpty(s)){ |
| | | log.error("未登录"); |
| | | Map<String,Integer> map = new HashMap<>(); |
| | | map.put("status",1); |
| | | pushMessage(session,JSONUtil.toJsonStr(map)); |
| | | closeSession(session,"没有订阅消息"); |
| | | closeSession(session,null); |
| | | } |
| | | threadLocalData.put(session.getId(), bean); |
| | | } |
| | |
| | | long lastMessageTime = lastMessageTimeMap.getOrDefault(session, 0L); |
| | | int time = wsBo.getTime(); |
| | | |
| | | message = megFiltration(wsBo,message); |
| | | if (currentTime - lastMessageTime >= time * 1000) { |
| | | // 时间间隔达到要求,可以发送消息 |
| | | if(wsBo.getPushNow()){ |
| | | message = megFiltration(wsBo,message); |
| | | pushMessage(session, message); |
| | | lastMessageTimeMap.put(session, currentTime); // 更新最后发送时间 |
| | | } else { |
| | | // 时间间隔未达到,不发送消息,可以记录日志或者其他操作 |
| | | // log.info("距离上次发送消息时间未达到指定间隔,不发送消息。"); |
| | | wsBo.setPushNow(false); |
| | | }else{ |
| | | message = megFiltration(wsBo,message); |
| | | if (currentTime - lastMessageTime >= time * 1000) { |
| | | // 时间间隔达到要求,可以发送消息 |
| | | pushMessage(session, message); |
| | | lastMessageTimeMap.put(session, currentTime); // 更新最后发送时间 |
| | | } |
| | | } |
| | | }else{ |
| | | pushMessage(session,"没有订阅消息"); |
| | | } |
| | | } |
| | | private static final Gson gson = new Gson(); |
| | |
| | | } |
| | | if (!CollectionUtils.isEmpty(currencies)) { |
| | | Set<String> filtrationSet = currencies.stream() |
| | | .map(f -> f.getCurrency() + f.getBuy() + f.getSell()) |
| | | // .map(f -> f.getCurrency() + f.getBuy() + f.getSell()) //组合过滤 ,暂时不使用,直接过滤整个币种 |
| | | .map(f -> f.getCurrency()) |
| | | .collect(Collectors.toSet()); |
| | | redisValueMap.removeIf(data -> filtrationSet.contains(data.getBuyAndSell())); |
| | | redisValueMap.removeIf(data -> filtrationSet.contains(data.getBaseAsset())); |
| | | } |
| | | |
| | | |
| | |
| | | } |
| | | //价差 |
| | | redisValueMap = redisValueMap.stream() |
| | | .filter(data -> Double.parseDouble(data.getSpread()) >= wsBo.getSpread()) |
| | | .filter(data -> Double.parseDouble(data.getSpread()) >= wsBo.getSpread() && Double.parseDouble(data.getSpread()) <= 1000) |
| | | .collect(Collectors.toList()); |
| | | |
| | | //最低金额 |
| | | if(null != wsBo.getMinAmount()){ |
| | | redisValueMap = redisValueMap.stream() |
| | |
| | | |
| | | // 关闭会话的方法 |
| | | private void closeSession(Session session, String reason) { |
| | | try { |
| | | threadLocalData.remove(session.getId()); |
| | | session.close(new CloseReason(CloseReason.CloseCodes.UNEXPECTED_CONDITION, reason)); |
| | | } catch (IOException e) { |
| | | log.error("强制断开连接----异常: {}", e.getMessage()); |
| | | } |
| | | wsServers.remove(this); |
| | | log.info(reason); |
| | | onClose(); |
| | | } |
| | | } |