From 4385b06801ad4c50bf4fc58bee992ef9de9fb260 Mon Sep 17 00:00:00 2001
From: xiaoyong931011 <15274802129@163.com>
Date: Thu, 27 May 2021 18:34:25 +0800
Subject: [PATCH] Merge branch 'otc' of http://120.27.238.55:7000/r/exchange into otc

---
 src/main/java/com/xcong/excoin/netty/logic/WebSocketLogic.java |   53 +++++++++++++++++++++++++++++++++--------------------
 1 files changed, 33 insertions(+), 20 deletions(-)

diff --git a/src/main/java/com/xcong/excoin/netty/logic/WebSocketLogic.java b/src/main/java/com/xcong/excoin/netty/logic/WebSocketLogic.java
index f1034c8..b2788cb 100644
--- a/src/main/java/com/xcong/excoin/netty/logic/WebSocketLogic.java
+++ b/src/main/java/com/xcong/excoin/netty/logic/WebSocketLogic.java
@@ -6,6 +6,7 @@
 import com.alibaba.fastjson.JSONObject;
 import com.xcong.excoin.common.contants.AppContants;
 import com.xcong.excoin.configurations.properties.SecurityProperties;
+import com.xcong.excoin.modules.member.entity.MemberEntity;
 import com.xcong.excoin.netty.bean.ChatRequest;
 import com.xcong.excoin.netty.bean.RequestBean;
 import com.xcong.excoin.netty.bean.ResponseBean;
@@ -41,15 +42,18 @@
         responseBean.setType(requestBean.getType());
         responseBean.setStatus(1);
 
-//        String bearerToken = requestBean.getData().toString();
-//        String rsaToken = bearerToken.replace(AppContants.TOKEN_START_WITH, "");
-//        RSA rsa = new RSA(securityProperties.getPrivateKey(), null);
-//        String[] tokens = StrUtil.split(rsa.decryptStr(rsaToken, KeyType.PrivateKey), "_");
-//
-//        Long memberId = Long.parseLong(tokens[0]);
+        String token = requestBean.getData().toString();
+        String redisKey = AppContants.APP_LOGIN_PREFIX + token;
+        String loginStr = redisUtils.getString(redisKey);
+        if (StrUtil.isBlank(loginStr)) {
+            ResponseBean res = ResponseBean.fail();
+            res.setType(requestBean.getType());
+            channel.writeAndFlush(NettyTools.webSocketBytes(JSONObject.toJSONString(res)));
+            return;
+        }
 
-
-        Long memberId = Long.parseLong(requestBean.getData().toString());
+        MemberEntity loginUser = JSONObject.parseObject(loginStr, MemberEntity.class);
+        Long memberId = loginUser.getId();
 
         channel.writeAndFlush(NettyTools.webSocketBytes(JSONObject.toJSONString(responseBean)));
         ChannelManager.addWsChannel(channel, memberId);
@@ -65,23 +69,32 @@
 
     public void sendMsg(RequestBean requestBean) {
         String chatStr = requestBean.getData().toString();
+        log.info("接收到的消息:{}", chatStr);
         ChatRequest chat = JSONObject.parseObject(chatStr, ChatRequest.class);
 
-        // 判断是否在线
-        Channel targetChannel = ChannelManager.findWsChannel(Long.parseLong(chat.getTo()));
-        if (targetChannel != null) {
-            targetChannel.writeAndFlush(NettyTools.webSocketBytes(JSONObject.toJSONString(ResponseBean.ok(chat))));
+        Channel channel = ChannelManager.findWebSocketChannel(requestBean.getChannelId());
+        ResponseBean res = ResponseBean.ok(chat);
+        chat.setTimestamp(System.currentTimeMillis());
+        res.setType(2);
+        channel.writeAndFlush(NettyTools.webSocketJson(res));
 
-            chatProducer.sendMsgHistory(chat);
+        // 判断是否在线
+        Channel targetChannel = ChannelManager.findWsChannel(chat.getTargetId());
+        if (targetChannel != null) {
+            chat.setIsSelf(2);
+            ResponseBean toRes = ResponseBean.ok(chat);
+            toRes.setType(2);
+            targetChannel.writeAndFlush(NettyTools.webSocketJson(toRes));
         } else {
             // 在redis中保存用户未在线时,给该用户发送的消息条数
-            String key = AppContants.MSG_NOTICE + chat.getTo();
-            String value = redisUtils.getString(key);
-            if (StrUtil.isEmpty(value)) {
-                redisUtils.set(key , 1);
-            } else {
-                redisUtils.set(key, Integer.parseInt(value) + 1);
-            }
+//            String key = AppContants.MSG_NOTICE + chat.getTargetId();
+//            String value = redisUtils.getString(key);
+//            if (StrUtil.isEmpty(value)) {
+//                redisUtils.set(key , 1);
+//            } else {
+//                redisUtils.set(key, Integer.parseInt(value) + 1);
+//            }
         }
+        chatProducer.sendMsgHistory(chat);
     }
 }

--
Gitblit v1.9.1