Prechádzať zdrojové kódy

申请添加好友增加WS,支持实时红点提醒

wangqi49 4 mesiacov pred
rodič
commit
2093686161

+ 2 - 1
src/main/java/com/webchat/common/enums/ChatMessageTypeEnum.java

@@ -12,7 +12,8 @@ public enum ChatMessageTypeEnum {
     CHAT_TEXT(1, "聊天"),
     CHAT_FILE(2, "文件"),
     RED_PACKET(3, "红包"),
-    PUBLIC_ACCOUNT_ARTICLE(4, "公众号推文");
+    PUBLIC_ACCOUNT_ARTICLE(4, "公众号推文"),
+    APPLY(5, "申请添加好友");
 
     private Integer type;
     private String desc;

+ 1 - 1
src/main/java/com/webchat/common/enums/RedisMessageChannelTopicEnum.java

@@ -5,7 +5,7 @@ import lombok.Getter;
 @Getter
 public enum RedisMessageChannelTopicEnum {
 
-    CHAT, PUSH_ARTICLE;
+    CHAT, PUSH_ARTICLE, APPLY;
 
     public String getChannel() {
         return this.name();

+ 4 - 0
src/main/java/com/webchat/config/configuration/RedisConfig.java

@@ -1,6 +1,7 @@
 package com.webchat.config.configuration;
 
 import com.webchat.common.enums.RedisMessageChannelTopicEnum;
+import com.webchat.service.listener.RedisApplyMessageListener;
 import com.webchat.service.listener.RedisChatMessageListener;
 import com.webchat.service.listener.RedisPushMessageListener;
 import org.springframework.context.annotation.Bean;
@@ -18,6 +19,8 @@ public class RedisConfig {
     private RedisChatMessageListener chatMessageListener;
     @Resource
     private RedisPushMessageListener pushMessageListener;
+    @Resource
+    private RedisApplyMessageListener redisApplyMessageListener;
 
     @Bean
     public RedisMessageListenerContainer container(RedisConnectionFactory redisConnectionFactory) {
@@ -27,6 +30,7 @@ public class RedisConfig {
         // 配置消息监听器
         container.addMessageListener(chatMessageListener, new ChannelTopic(RedisMessageChannelTopicEnum.CHAT.getChannel()));
         container.addMessageListener(pushMessageListener, new ChannelTopic(RedisMessageChannelTopicEnum.PUSH_ARTICLE.getChannel()));
+        container.addMessageListener(redisApplyMessageListener, new ChannelTopic(RedisMessageChannelTopicEnum.APPLY.getChannel()));
         return container;
     }
 

+ 6 - 0
src/main/java/com/webchat/service/FriendService.java

@@ -1,12 +1,14 @@
 package com.webchat.service;
 
 import com.webchat.common.enums.FriendStatusEnum;
+import com.webchat.common.enums.RedisMessageChannelTopicEnum;
 import com.webchat.common.enums.RoleCodeEnum;
 import com.webchat.common.exception.BusinessException;
 import com.webchat.domain.vo.response.FriendApplyUserVO;
 import com.webchat.domain.vo.response.UserBaseResponseInfoVO;
 import com.webchat.repository.dao.IFriendDAO;
 import com.webchat.repository.entity.FriendEntity;
+import com.webchat.service.listener.RedisMessageSender;
 import org.apache.commons.collections.CollectionUtils;
 import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -33,6 +35,8 @@ public class FriendService {
     private UserService userService;
     @Autowired
     private RedisService redisService;
+    @Autowired
+    private RedisMessageSender redisMessageSender;
 
     /**
      * 申请添加好友
@@ -65,6 +69,8 @@ public class FriendService {
             }
         }
         friendDAO.save(friendEntity);
+        // 申请添加好友,利用广播模式,消息广播到所有节点,实现红点实时提醒
+        redisMessageSender.sendMessage(RedisMessageChannelTopicEnum.APPLY.getChannel(), friendId);
         return true;
     }
 

+ 62 - 0
src/main/java/com/webchat/service/listener/RedisApplyMessageListener.java

@@ -0,0 +1,62 @@
+package com.webchat.service.listener;
+
+import com.webchat.common.enums.ChatMessageTypeEnum;
+import com.webchat.common.util.JsonUtil;
+import com.webchat.controller.client.ChatWebSocket;
+import com.webchat.domain.vo.response.UserBaseResponseInfoVO;
+import com.webchat.domain.vo.response.mess.ChatMessageResponseVO;
+import com.webchat.domain.vo.response.mess.PublicAccountArticleMessageVO;
+import com.webchat.domain.vo.response.publicaccount.ArticleBaseResponseVO;
+import com.webchat.service.FriendService;
+import com.webchat.service.UserService;
+import com.webchat.service.publicaccount.ArticleService;
+import com.webchat.service.queue.dto.ArticleDelayConsumeMessageDTO;
+import lombok.extern.slf4j.Slf4j;
+import okhttp3.WebSocket;
+import org.apache.commons.collections.CollectionUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Qualifier;
+import org.springframework.data.redis.connection.Message;
+import org.springframework.data.redis.connection.MessageListener;
+import org.springframework.data.redis.core.RedisTemplate;
+import org.springframework.stereotype.Component;
+
+import javax.websocket.Session;
+import java.util.Map;
+import java.util.Set;
+
+@Slf4j
+@Component
+public class RedisApplyMessageListener implements MessageListener {
+
+    private final RedisTemplate redisTemplate;
+
+    public RedisApplyMessageListener(@Qualifier("redisTemplate") RedisTemplate redisTemplate) {
+        this.redisTemplate = redisTemplate;
+    }
+
+    @Override
+    public void onMessage(Message message, byte[] bytes) {
+        String channel = (String) redisTemplate.getStringSerializer().deserialize(message.getChannel());
+        String messageStr = (String) redisTemplate.getValueSerializer().deserialize(message.getBody());
+        log.info("redis message listener ======> channel:{}, message:{}", channel, messageStr);
+        this.notifyApplyMessageByWebSession(messageStr);
+    }
+
+
+    private void notifyApplyMessageByWebSession(String receiver) {
+
+        ChatWebSocket ws = ChatWebSocket.clients.get(receiver);
+        if (ws == null) {
+            return;
+        }
+        Session session = ws.getSession();
+        if (session == null || !session.isOpen()) {
+            return;
+        }
+        ChatMessageResponseVO chatMessageResponseVO = new ChatMessageResponseVO();
+        chatMessageResponseVO.setType(ChatMessageTypeEnum.APPLY.getType());
+        session.getAsyncRemote().sendText(JsonUtil.toJsonString(chatMessageResponseVO));
+    }
+
+}

+ 27 - 6
src/main/resources/banner.txt

@@ -1,6 +1,27 @@
-__          __  _        _____ _           _   
-\ \        / / | |      / ____| |         | |  
- \ \  /\  / /__| |__   | |    | |__   __ _| |_ 
-  \ \/  \/ / _ \ '_ \  | |    | '_ \ / _` | __|
-   \  /\  /  __/ |_) | | |____| | | | (_| | |_ 
-    \/  \/ \___|_.__/   \_____|_| |_|\__,_|\__|
+        __          __  _        _____ _           _
+        \ \        / / | |      / ____| |         | |
+         \ \  /\  / /__| |__   | |    | |__   __ _| |_
+          \ \/  \/ / _ \ '_ \  | |    | '_ \ / _` | __|
+           \  /\  /  __/ |_) | | |____| | | | (_| | |_
+            \/  \/ \___|_.__/   \_____|_| |_|\__,_|\__|
+
+//                          _ooOoo_                               //
+//                         o8888888o                              //
+//                         88" . "88                              //
+//                         (| ^_^ |)                              //
+//                         O\  =  /O                              //
+//                      ____/`---'\____                           //
+//                    .'  \\|     |//  `.                         //
+//                   /  \\|||  :  |||//  \                        //
+//                  /  _||||| -:- |||||-  \                       //
+//                  |   | \\\  -  /// |   |                       //
+//                  | \_|  ''\---/''  |   |                       //
+//                  \  .-\__  `-`  ___/-. /                       //
+//                ___`. .'  /--.--\  `. . ___                     //
+//              ."" '<  `.___\_<|>_/___.'  >'"".                  //
+//            | | :  `- \`.;`\ _ /`;.`/ - ` : | |                 //
+//            \  \ `-.   \_ __\ /__ _/   .-` /  /                 //
+//      ========`-.____`-.___\_____/___.-`____.-'========         //
+//                           `=---='                              //
+//      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^        //
+//             佛祖保佑       永不宕机      永无BUG                  //

+ 2 - 0
src/main/resources/static/js/client/chat.js

@@ -440,6 +440,7 @@ function applyPass(id) {
             data = eval(data);
             if (data.success){
                 layer.msg("通过");
+                $(".new-message-icon").hide();
                 $("#apply-item-"+id).fadeOut(500);
             } else {
                 layer.msg("服务异常");
@@ -456,6 +457,7 @@ function applyRefuse(id) {
             data = eval(data);
             if (data.success){
                 layer.msg("已拒绝");
+                $(".new-message-icon").hide();
                 $("#apply-item-"+id).fadeOut(500);
             } else {
                 layer.msg("服务异常");

+ 4 - 3
src/main/resources/templates/client/chat.html

@@ -178,9 +178,6 @@
     function initWebSocket() {
         if ("WebSocket" in window) {
             webSocket = new WebSocket("ws://"+wsHost+":"+wsPort+"/ws/chat/" + userId);
-            webSocket.onopen = function () {
-                console.log("已经连通了websocket");
-            };
             // 发送心跳函数
             function sendHeartbeat() {
                 if (webSocket.readyState === WebSocket.OPEN) {
@@ -220,6 +217,10 @@
                 } else if (mess.rejectVideoOffer) {
                     hideVideoOfferNotify();
                     return;
+                } else if (mess.type == 5) {
+                    // 申请添加好友消息提醒,显示红点信息
+                    $(".new-message-icon").show();
+                    return;
                 }
                 showReceiverMsg(mess);
                 setTimeout('loadChat(false)', 1000);

BIN
static/upload/image/329b617d9f4e484aafba0af295bca37a.png


BIN
static/upload/image/4492949512444ebcb08aaeab4261e363.png


BIN
static/upload/image/7752df9a8d23434eadf77e81dfcf0f74.png