From ce4299cb54b1fdecac72635c439cc10e33ad8186 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=96=AF=E7=8B=82=E7=9A=84=E7=8B=AE=E5=AD=90Li?= <15040126243@163.com> Date: Thu, 2 Nov 2023 14:22:18 +0800 Subject: [PATCH] =?UTF-8?q?update=20=E4=BC=98=E5=8C=96=20websocket=20?= =?UTF-8?q?=E6=A8=A1=E5=9D=97=E5=87=8F=E5=B0=91=E6=97=A5=E5=BF=97=E8=BE=93?= =?UTF-8?q?=E5=87=BA=20=E5=A2=9E=E5=8A=A0=E7=99=BB=E5=BD=95=E6=8E=A8?= =?UTF-8?q?=E9=80=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ruoyi-auth/pom.xml | 4 ++++ .../dromara/auth/controller/TokenController.java | 15 ++++++++++++++- .../websocket/handler/PlusWebSocketHandler.java | 1 - .../common/websocket/utils/WebSocketUtils.java | 9 ++------- 4 files changed, 20 insertions(+), 9 deletions(-) diff --git a/ruoyi-auth/pom.xml b/ruoyi-auth/pom.xml index 2c17437b..b4f8b202 100644 --- a/ruoyi-auth/pom.xml +++ b/ruoyi-auth/pom.xml @@ -89,6 +89,10 @@ + + org.dromara + ruoyi-api-resource + diff --git a/ruoyi-auth/src/main/java/org/dromara/auth/controller/TokenController.java b/ruoyi-auth/src/main/java/org/dromara/auth/controller/TokenController.java index 8a3e896b..f7549a93 100644 --- a/ruoyi-auth/src/main/java/org/dromara/auth/controller/TokenController.java +++ b/ruoyi-auth/src/main/java/org/dromara/auth/controller/TokenController.java @@ -22,10 +22,12 @@ import org.dromara.common.core.domain.R; import org.dromara.common.core.domain.model.LoginBody; import org.dromara.common.core.utils.*; import org.dromara.common.json.utils.JsonUtils; +import org.dromara.common.satoken.utils.LoginHelper; import org.dromara.common.social.config.properties.SocialLoginConfigProperties; import org.dromara.common.social.config.properties.SocialProperties; import org.dromara.common.social.utils.SocialUtils; import org.dromara.common.tenant.helper.TenantHelper; +import org.dromara.resource.api.RemoteMessageService; import org.dromara.system.api.RemoteClientService; import org.dromara.system.api.RemoteConfigService; import org.dromara.system.api.RemoteSocialService; @@ -37,6 +39,8 @@ import org.springframework.web.bind.annotation.*; import java.net.URL; import java.util.List; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; /** * token 控制 @@ -51,6 +55,7 @@ public class TokenController { private final SocialProperties socialProperties; private final SysLoginService sysLoginService; + private final ScheduledExecutorService scheduledExecutorService; @DubboReference private final RemoteConfigService remoteConfigService; @@ -60,6 +65,8 @@ public class TokenController { private final RemoteClientService remoteClientService; @DubboReference private final RemoteSocialService remoteSocialService; + @DubboReference + private final RemoteMessageService remoteMessageService; /** * 登录方法 @@ -86,7 +93,13 @@ public class TokenController { // 校验租户 sysLoginService.checkTenant(loginBody.getTenantId()); // 登录 - return R.ok(IAuthStrategy.login(body, clientVo, grantType)); + LoginVo loginVo = IAuthStrategy.login(body, clientVo, grantType); + + Long userId = LoginHelper.getUserId(); + scheduledExecutorService.schedule(() -> { + remoteMessageService.sendMessage(userId, "欢迎登录RuoYi-Cloud-Plus微服务管理系统"); + }, 3, TimeUnit.SECONDS); + return R.ok(loginVo); } /** diff --git a/ruoyi-common/ruoyi-common-websocket/src/main/java/org/dromara/common/websocket/handler/PlusWebSocketHandler.java b/ruoyi-common/ruoyi-common-websocket/src/main/java/org/dromara/common/websocket/handler/PlusWebSocketHandler.java index 84d8178f..b92f0cf5 100644 --- a/ruoyi-common/ruoyi-common-websocket/src/main/java/org/dromara/common/websocket/handler/PlusWebSocketHandler.java +++ b/ruoyi-common/ruoyi-common-websocket/src/main/java/org/dromara/common/websocket/handler/PlusWebSocketHandler.java @@ -40,7 +40,6 @@ public class PlusWebSocketHandler extends AbstractWebSocketHandler { @Override protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception { LoginUser loginUser = (LoginUser) session.getAttributes().get(LOGIN_USER_KEY); - log.info("PlusWebSocketHandler, 连接:" + session.getId() + ",已收到消息:" + message.getPayload()); List userIds = List.of(loginUser.getUserId()); WebSocketMessageDto webSocketMessageDto = new WebSocketMessageDto(); webSocketMessageDto.setSessionKeys(userIds); diff --git a/ruoyi-common/ruoyi-common-websocket/src/main/java/org/dromara/common/websocket/utils/WebSocketUtils.java b/ruoyi-common/ruoyi-common-websocket/src/main/java/org/dromara/common/websocket/utils/WebSocketUtils.java index 3c5a63c3..3b3e9681 100644 --- a/ruoyi-common/ruoyi-common-websocket/src/main/java/org/dromara/common/websocket/utils/WebSocketUtils.java +++ b/ruoyi-common/ruoyi-common-websocket/src/main/java/org/dromara/common/websocket/utils/WebSocketUtils.java @@ -7,7 +7,6 @@ import lombok.extern.slf4j.Slf4j; import org.dromara.common.redis.utils.RedisUtils; import org.dromara.common.websocket.dto.WebSocketMessageDto; import org.dromara.common.websocket.holder.WebSocketSessionHolder; -import org.dromara.system.api.model.LoginUser; import org.springframework.web.socket.PongMessage; import org.springframework.web.socket.TextMessage; import org.springframework.web.socket.WebSocketMessage; @@ -18,7 +17,6 @@ import java.util.ArrayList; import java.util.List; import java.util.function.Consumer; -import static org.dromara.common.websocket.constant.WebSocketConstants.LOGIN_USER_KEY; import static org.dromara.common.websocket.constant.WebSocketConstants.WEB_SOCKET_TOPIC; /** @@ -71,7 +69,7 @@ public class WebSocketUtils { broadcastMessage.setMessage(webSocketMessage.getMessage()); broadcastMessage.setSessionKeys(unsentSessionKeys); RedisUtils.publish(WEB_SOCKET_TOPIC, broadcastMessage, consumer -> { - log.info(" WebSocket发送主题订阅消息topic:{} session keys:{} message:{}", + log.info("WebSocket发送主题订阅消息topic:{} session keys:{} message:{}", WEB_SOCKET_TOPIC, unsentSessionKeys, webSocketMessage.getMessage()); }); } @@ -89,7 +87,7 @@ public class WebSocketUtils { WebSocketMessageDto broadcastMessage = new WebSocketMessageDto(); broadcastMessage.setMessage(message); RedisUtils.publish(WEB_SOCKET_TOPIC, broadcastMessage, consumer -> { - log.info(" WebSocket发送主题订阅消息topic:{} message:{}", WEB_SOCKET_TOPIC, message); + log.info("WebSocket发送主题订阅消息topic:{} message:{}", WEB_SOCKET_TOPIC, message); }); } @@ -106,10 +104,7 @@ public class WebSocketUtils { log.error("[send] session会话已经关闭"); } else { try { - // 获取当前会话中的用户 - LoginUser loginUser = (LoginUser) session.getAttributes().get(LOGIN_USER_KEY); session.sendMessage(message); - log.info("[send] sessionId: {},userId:{},userType:{},message:{}", session.getId(), loginUser.getUserId(), loginUser.getUserType(), message); } catch (IOException e) { log.error("[send] session({}) 发送消息({}) 异常", session, message, e); }