From 0792c2a65171eb7a763ac74cd4546552b7e0c756 Mon Sep 17 00:00:00 2001
From: Helius <wangdoubleone@gmail.com>
Date: Thu, 27 May 2021 17:21:27 +0800
Subject: [PATCH] modify

---
 src/main/java/com/xcong/excoin/netty/logic/WebSocketLogic.java |   21 ++++++++++++++-------
 1 files changed, 14 insertions(+), 7 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 8378aa7..8511435 100644
--- a/src/main/java/com/xcong/excoin/netty/logic/WebSocketLogic.java
+++ b/src/main/java/com/xcong/excoin/netty/logic/WebSocketLogic.java
@@ -76,19 +76,25 @@
 
     public void sendMsg(RequestBean requestBean) {
         String chatStr = requestBean.getData().toString();
+        log.info("接收到的消息:{}", chatStr);
         ChatRequest chat = JSONObject.parseObject(chatStr, ChatRequest.class);
 
         Channel channel = ChannelManager.findWebSocketChannel(requestBean.getChannelId());
-        channel.writeAndFlush(NettyTools.webSocketJson(ResponseBean.ok(chat)));
-        // 判断是否在线
-        Channel targetChannel = ChannelManager.findWsChannel(Long.parseLong(chat.getTo()));
-        if (targetChannel != null) {
-            targetChannel.writeAndFlush(NettyTools.webSocketBytes(JSONObject.toJSONString(ResponseBean.ok(chat))));
+        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);
+            res.setType(2);
+            targetChannel.writeAndFlush(NettyTools.webSocketJson(toRes));
         } else {
             // 在redis中保存用户未在线时,给该用户发送的消息条数
-            String key = AppContants.MSG_NOTICE + chat.getTo();
+            String key = AppContants.MSG_NOTICE + chat.getTargetId();
             String value = redisUtils.getString(key);
             if (StrUtil.isEmpty(value)) {
                 redisUtils.set(key , 1);
@@ -96,5 +102,6 @@
                 redisUtils.set(key, Integer.parseInt(value) + 1);
             }
         }
+        chatProducer.sendMsgHistory(chat);
     }
 }

--
Gitblit v1.9.1