From 182c01b109ffb3b4be248c2e128835ace70eeae8 Mon Sep 17 00:00:00 2001
From: Helius <wangdoubleone@gmail.com>
Date: Thu, 27 May 2021 15:53:54 +0800
Subject: [PATCH] add websocket

---
 src/main/java/com/xcong/excoin/netty/logic/WebSocketLogic.java |   61 +++++++++++++++++++++++++++++-
 1 files changed, 58 insertions(+), 3 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 16b6f52..f1034c8 100644
--- a/src/main/java/com/xcong/excoin/netty/logic/WebSocketLogic.java
+++ b/src/main/java/com/xcong/excoin/netty/logic/WebSocketLogic.java
@@ -1,11 +1,21 @@
 package com.xcong.excoin.netty.logic;
 
+import cn.hutool.core.util.StrUtil;
+import cn.hutool.crypto.asymmetric.KeyType;
+import cn.hutool.crypto.asymmetric.RSA;
 import com.alibaba.fastjson.JSONObject;
+import com.xcong.excoin.common.contants.AppContants;
+import com.xcong.excoin.configurations.properties.SecurityProperties;
+import com.xcong.excoin.netty.bean.ChatRequest;
 import com.xcong.excoin.netty.bean.RequestBean;
 import com.xcong.excoin.netty.bean.ResponseBean;
 import com.xcong.excoin.netty.common.ChannelManager;
 import com.xcong.excoin.netty.common.NettyTools;
+import com.xcong.excoin.rabbit.producer.ChatProducer;
+import com.xcong.excoin.utils.RedisUtils;
 import io.netty.channel.Channel;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
 
 
@@ -14,19 +24,64 @@
  * @email wangdoubleone@gmail.com
  * @date 2019-05-09
  */
+@Slf4j
 @Component
 public class WebSocketLogic {
 
+    @Autowired
+    private RedisUtils redisUtils;
+    @Autowired
+    private ChatProducer chatProducer;
+    @Autowired
+    private SecurityProperties securityProperties;
+
     public void authCheck(RequestBean requestBean) {
         Channel channel = ChannelManager.findWebSocketChannel(requestBean.getChannelId());
-
-        ChannelManager.addWsChannel(channel, Long.parseLong(requestBean.getData().toString()));
-
         ResponseBean responseBean = new ResponseBean();
         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]);
+
+
+        Long memberId = Long.parseLong(requestBean.getData().toString());
+
         channel.writeAndFlush(NettyTools.webSocketBytes(JSONObject.toJSONString(responseBean)));
+        ChannelManager.addWsChannel(channel, memberId);
+        String value = redisUtils.getString(AppContants.MSG_NOTICE + memberId);
+        if (StrUtil.isNotBlank(value)) {
+            responseBean.setType(3);
+            responseBean.setData(Integer.parseInt(value));
+            channel.writeAndFlush(NettyTools.webSocketBytes(JSONObject.toJSONString(responseBean)));
+
+            redisUtils.del(AppContants.MSG_NOTICE + memberId);
+        }
     }
 
+    public void sendMsg(RequestBean requestBean) {
+        String chatStr = requestBean.getData().toString();
+        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))));
+
+            chatProducer.sendMsgHistory(chat);
+        } 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);
+            }
+        }
+    }
 }

--
Gitblit v1.9.1