diff --git a/src/main/java/com/iflytop/gd/common/controller/NotificationWebsocketEndpoint.java b/src/main/java/com/iflytop/gd/common/controller/NotificationWebsocketEndpoint.java index d09e3a1..e2979c3 100644 --- a/src/main/java/com/iflytop/gd/common/controller/NotificationWebsocketEndpoint.java +++ b/src/main/java/com/iflytop/gd/common/controller/NotificationWebsocketEndpoint.java @@ -1,6 +1,6 @@ package com.iflytop.gd.common.controller; -import com.iflytop.gd.infrastructure.modules.WebSocketNotificationMgrImpl; +import com.iflytop.gd.infrastructure.modules.WebSocketNotificationPushMgrImpl; import jakarta.annotation.Resource; import jakarta.websocket.EndpointConfig; import jakarta.websocket.OnOpen; @@ -15,7 +15,7 @@ import org.springframework.stereotype.Component; public class NotificationWebsocketEndpoint { @Resource - private WebSocketNotificationMgrImpl notificationMgr; + private WebSocketNotificationPushMgrImpl notificationMgr; @OnOpen public void onOpen(Session session, EndpointConfig endpointConfig) { diff --git a/src/main/java/com/iflytop/gd/infrastructure/modules/WebSocketNotificationMgrImpl.java b/src/main/java/com/iflytop/gd/infrastructure/modules/WebSocketNotificationPushMgrImpl.java similarity index 55% rename from src/main/java/com/iflytop/gd/infrastructure/modules/WebSocketNotificationMgrImpl.java rename to src/main/java/com/iflytop/gd/infrastructure/modules/WebSocketNotificationPushMgrImpl.java index bd097c4..c2b8b22 100644 --- a/src/main/java/com/iflytop/gd/infrastructure/modules/WebSocketNotificationMgrImpl.java +++ b/src/main/java/com/iflytop/gd/infrastructure/modules/WebSocketNotificationPushMgrImpl.java @@ -2,22 +2,32 @@ package com.iflytop.gd.infrastructure.modules; import cn.hutool.json.JSONUtil; import com.iflytop.gd.common.notification.Notification; -import com.iflytop.gd.system.modules.NotificationMgr; +import com.iflytop.gd.system.modules.NotificationPushMgr; +import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Component; import jakarta.websocket.Session; +import java.io.IOException; import java.util.HashSet; import java.util.Set; +/** + * 使用Websocket实现通知推送管理 + */ +@Slf4j @Component -public class WebSocketNotificationMgrImpl implements NotificationMgr { +public class WebSocketNotificationPushMgrImpl implements NotificationPushMgr { private final Set sessions = new HashSet<>(); @Override public void pushNotification(Notification notification) { for (Session session : sessions) { - session.getAsyncRemote().sendText(JSONUtil.toJsonStr(notification)); + try { + session.getBasicRemote().sendText(JSONUtil.toJsonStr(notification)); + } catch (IOException e) { + log.error("Fail to send notification to session: {}", session.getId(), e); + } } } diff --git a/src/main/java/com/iflytop/gd/system/modules/NotificationMgr.java b/src/main/java/com/iflytop/gd/system/modules/NotificationPushMgr.java similarity index 50% rename from src/main/java/com/iflytop/gd/system/modules/NotificationMgr.java rename to src/main/java/com/iflytop/gd/system/modules/NotificationPushMgr.java index 036fb96..0b38bac 100644 --- a/src/main/java/com/iflytop/gd/system/modules/NotificationMgr.java +++ b/src/main/java/com/iflytop/gd/system/modules/NotificationPushMgr.java @@ -2,6 +2,14 @@ package com.iflytop.gd.system.modules; import com.iflytop.gd.common.notification.Notification; -public interface NotificationMgr { +/** + * 通知管理器 + */ +public interface NotificationPushMgr { + + /** + * 向前端推送通知 + * @param notification 通知实例 + */ void pushNotification(Notification notification); }