|
|
@ -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<Session> 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); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|