From b3a429204f02373172a380ff730dfbaf89de09a8 Mon Sep 17 00:00:00 2001 From: xd <844539747@qq.com> Date: Wed, 24 Apr 2024 16:54:52 +0800 Subject: [PATCH] '123' --- .../system/SysNoticeController.java | 30 ++-- .../websocket/OneToManyWebSocket.java | 132 ++++++++++++++++++ .../ruoyi/system/mapper/SysNoticeMapper.java | 9 +- .../system/service/ISysNoticeService.java | 9 +- .../service/impl/SysNoticeServiceImpl.java | 14 +- .../mapper/system/SysNoticeMapper.xml | 21 ++- .../src/layout/components/NavbarNotice.vue | 18 ++- 7 files changed, 208 insertions(+), 25 deletions(-) create mode 100644 ruoyi-framework/src/main/java/com/ruoyi/framework/websocket/OneToManyWebSocket.java diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysNoticeController.java b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysNoticeController.java index d559464..7bb2266 100644 --- a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysNoticeController.java +++ b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysNoticeController.java @@ -1,13 +1,11 @@ package com.ruoyi.web.controller.system; -import java.util.Collection; -import java.util.Comparator; -import java.util.List; -import java.util.Map; +import java.util.*; import java.util.stream.Collectors; import com.alibaba.fastjson2.JSONObject; import com.ruoyi.common.constant.WebsocketConst; +import com.ruoyi.framework.websocket.OneToManyWebSocket; import com.ruoyi.framework.websocket.WebSocketServer; import com.ruoyi.framework.websocket.WebSocketUsers; import com.ruoyi.system.domain.SysNoticeUser; @@ -45,7 +43,7 @@ public class SysNoticeController extends BaseController private ISysNoticeService noticeService; @Autowired - private WebSocketServer webSocketServer; + private OneToManyWebSocket oneToManyWebSocket; /** * 获取通知公告列表 @@ -78,19 +76,28 @@ public class SysNoticeController extends BaseController public AjaxResult add(@Validated @RequestBody SysNotice notice) { notice.setCreateBy(getUsername()); + noticeService.insertNotice(notice); + List sysNoticeUsers = new ArrayList(); JSONObject obj = new JSONObject(); obj.put(WebsocketConst.MSG_ID, notice.getNoticeId()); obj.put(WebsocketConst.MSG_TITLE, notice.getNoticeTitle()); obj.put(WebsocketConst.MSG_CONTENT, notice.getNoticeContent()); if(!"3".equals(notice.getNoticeType())){//系统通知 或 通知公告 - WebSocketUsers.sendMessageToUsersByText(obj.toString()); - SysNoticeUser sysNoticeUser = new SysNoticeUser(); - sysNoticeUser.setNoticeId(notice.getNoticeId()); - sysNoticeUser.setUserId(notice.getNoticeId()); + oneToManyWebSocket.sendMessage(obj.toString()); + + SysNoticeUser sysNoticeUser = null; + Map users = oneToManyWebSocket.getUsers(); + for(String userId:users.keySet()){ + sysNoticeUser = new SysNoticeUser(); + sysNoticeUser.setNoticeId(notice.getNoticeId()); + sysNoticeUser.setUserId(Long.valueOf(userId)); + sysNoticeUsers.add(sysNoticeUser); + } + noticeService.insertNoticeUserBatch(sysNoticeUsers); } - return toAjax(noticeService.insertNotice(notice)); + return success(); } /** @@ -123,7 +130,8 @@ public class SysNoticeController extends BaseController @GetMapping("/navbarNoticelist") public Map> navbarNoticelist() { - List list = noticeService.navbarNoticelist(); + Long userId = getLoginUser().getUserId();//当前登陆者 + List list = noticeService.navbarNoticelist(userId); Map> groupedByNoticeType = list.stream() .collect(Collectors.groupingBy(SysNotice::getNoticeType)); for(String key:groupedByNoticeType.keySet()){ diff --git a/ruoyi-framework/src/main/java/com/ruoyi/framework/websocket/OneToManyWebSocket.java b/ruoyi-framework/src/main/java/com/ruoyi/framework/websocket/OneToManyWebSocket.java new file mode 100644 index 0000000..e297f94 --- /dev/null +++ b/ruoyi-framework/src/main/java/com/ruoyi/framework/websocket/OneToManyWebSocket.java @@ -0,0 +1,132 @@ +package com.ruoyi.framework.websocket; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Component; + +import javax.websocket.*; +import javax.websocket.server.PathParam; +import javax.websocket.server.ServerEndpoint; +import java.util.List; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.atomic.AtomicInteger; + +/** + * + * 前后端交互的类实现消息的接收推送(自己发送给所有人(不包括自己)) + * + */ +@ServerEndpoint(value = "/websocket/message/{userId}") +@Component +public class OneToManyWebSocket { + /** + * WebSocketUsers 日志控制器 + */ + private static final Logger LOGGER = LoggerFactory.getLogger(WebSocketUsers.class); + + /** 记录当前在线连接数 */ + private static AtomicInteger onlineCount = new AtomicInteger(0); + + /** 存放所有在线的客户端 key 为用户Id */ + private static final Map clients = new ConcurrentHashMap<>(); + + /** + * 连接建立成功调用的方法 + */ + @OnOpen + public void onOpen(Session session,@PathParam("userId") String userId) { + if(clients.containsKey(userId)){ + clients.remove(userId); + clients.put(userId, session); + }else{ + onlineCount.incrementAndGet(); // 在线数加1 + clients.put(userId, session); + //sendMessage("你好啊客户端"+userId,session); + } + } + + /** + * 连接关闭调用的方法 + */ + @OnClose + public void onClose(Session session) { + for (String userId : clients.keySet()) { + if(clients.get(userId).equals(session)){ + clients.remove(session); + onlineCount.decrementAndGet(); // 在线数减1 + } + } + } + + /** + * 收到客户端消息后调用的方法 + * + * @param message + * 客户端发送过来的消息 + */ + @OnMessage + public void onMessage(String message, Session session) { + LOGGER.info("服务端收到客户端[{}]的消息:{}", session.getId(), message); + //this.sendMessage(message, session); + } + + @OnError + public void onError(Session session, Throwable error) { + LOGGER.error("发生错误"); + error.printStackTrace(); + } + + /** + * 获取在线用户列表 + * + * @return 返回用户集合 + */ + public static Map getUsers() + { + return clients; + } + + + /** + * 群发消息 + * + * @param message + */ + public static void sendMessage(String message) { + for (Map.Entry sessionEntry : clients.entrySet()) { + Session toSession = sessionEntry.getValue(); + // 排除掉自己 + //if (!fromSession.getId().equals(toSession.getId())) { + LOGGER.info("服务端给客户端[{}]发送消息{}", toSession.getId(), message); + toSession.getAsyncRemote().sendText(message); + //} + } + } + + /** + * 群发消息 + * + * @param message + * 消息内容 + */ + public static void sendMessage(String message, List ids) { + + for (String id : ids) { + Session session = clients.get(id); + if(session!=null){ + LOGGER.info("服务端给客户端[{}]发送消息{}", session.getId(), message); + try { + session.getAsyncRemote().sendText(message); + }catch (Exception e){ + LOGGER.info("数据发送失败!疑似断开连接", session.getId(), message); + clients.remove(id); + } + } + } + } + + public Session getUserSession(String userId){ + return clients.get(userId); + } +} diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/mapper/SysNoticeMapper.java b/ruoyi-system/src/main/java/com/ruoyi/system/mapper/SysNoticeMapper.java index c762e38..5f80885 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/system/mapper/SysNoticeMapper.java +++ b/ruoyi-system/src/main/java/com/ruoyi/system/mapper/SysNoticeMapper.java @@ -2,6 +2,7 @@ package com.ruoyi.system.mapper; import java.util.List; import com.ruoyi.system.domain.SysNotice; +import com.ruoyi.system.domain.SysNoticeUser; /** * 通知公告表 数据层 @@ -62,5 +63,11 @@ public interface SysNoticeMapper * 消息推送信息获取 * @return */ - List navbarNoticelist(); + List navbarNoticelist(Long userId); + + /** + * 批量插入消息-用户 中间表 + * @param sysNoticeUsers + */ + void insertNoticeUserBatch(List sysNoticeUsers); } diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/service/ISysNoticeService.java b/ruoyi-system/src/main/java/com/ruoyi/system/service/ISysNoticeService.java index bed1e32..891f1e9 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/system/service/ISysNoticeService.java +++ b/ruoyi-system/src/main/java/com/ruoyi/system/service/ISysNoticeService.java @@ -2,6 +2,7 @@ package com.ruoyi.system.service; import java.util.List; import com.ruoyi.system.domain.SysNotice; +import com.ruoyi.system.domain.SysNoticeUser; /** * 公告 服务层 @@ -62,5 +63,11 @@ public interface ISysNoticeService * 导航面板 消息通知 * @return */ - List navbarNoticelist(); + List navbarNoticelist(Long userId); + + /** + * 批量插入消息-用户 中间表 + * @param sysNoticeUsers + */ + void insertNoticeUserBatch(List sysNoticeUsers); } diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysNoticeServiceImpl.java b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysNoticeServiceImpl.java index 726d768..7b552af 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysNoticeServiceImpl.java +++ b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysNoticeServiceImpl.java @@ -4,6 +4,7 @@ import java.nio.charset.StandardCharsets; import java.util.List; import com.ruoyi.common.utils.StringUtils; +import com.ruoyi.system.domain.SysNoticeUser; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.ruoyi.system.domain.SysNotice; @@ -118,7 +119,16 @@ public class SysNoticeServiceImpl implements ISysNoticeService * @return */ @Override - public List navbarNoticelist() { - return noticeMapper.navbarNoticelist(); + public List navbarNoticelist(Long userId) { + return noticeMapper.navbarNoticelist(userId); + } + + /** + * 批量插入消息-用户 中间表 + * @param sysNoticeUsers + */ + @Override + public void insertNoticeUserBatch(List sysNoticeUsers) { + noticeMapper.insertNoticeUserBatch(sysNoticeUsers); } } diff --git a/ruoyi-system/src/main/resources/mapper/system/SysNoticeMapper.xml b/ruoyi-system/src/main/resources/mapper/system/SysNoticeMapper.xml index 2b3fc06..d450174 100644 --- a/ruoyi-system/src/main/resources/mapper/system/SysNoticeMapper.xml +++ b/ruoyi-system/src/main/resources/mapper/system/SysNoticeMapper.xml @@ -42,7 +42,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" - + insert into sys_notice ( notice_title, notice_type, @@ -87,11 +87,26 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" + + insert into sys_user_notice + (noticeId,userId) + values + + ( + #{item.noticeId,jdbcType=INTEGER}, + #{item.userId,jdbcType=INTEGER} + ) + + + diff --git a/ruoyi-ui/src/layout/components/NavbarNotice.vue b/ruoyi-ui/src/layout/components/NavbarNotice.vue index 53835be..2904826 100644 --- a/ruoyi-ui/src/layout/components/NavbarNotice.vue +++ b/ruoyi-ui/src/layout/components/NavbarNotice.vue @@ -26,10 +26,11 @@