feat(property): 用 Server-Sent Events (SSE) 替代 WebSocket 实现消息推送
This commit is contained in:
@@ -132,6 +132,34 @@ public class SseEmitterManager {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 向指定的用户的指定会话发送消息
|
||||
*
|
||||
* @param userId 要发送消息的用户id
|
||||
* @param token 用户的会话令牌
|
||||
* @param message 要发送的消息内容
|
||||
*/
|
||||
public void sendMessage(Long userId, String token, String message) {
|
||||
Map<String, SseEmitter> emitters = USER_TOKEN_EMITTERS.get(userId);
|
||||
if (MapUtil.isNotEmpty(emitters)) {
|
||||
for (Map.Entry<String, SseEmitter> entry : emitters.entrySet()){
|
||||
if (entry.getKey().equals(token)) {
|
||||
try {
|
||||
entry.getValue().send(SseEmitter.event()
|
||||
.name("message")
|
||||
.data(message));
|
||||
} catch (Exception e) {
|
||||
SseEmitter remove = emitters.remove(entry.getKey());
|
||||
if (remove != null) {
|
||||
remove.complete();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 本机全用户会话发送消息
|
||||
*
|
||||
|
@@ -38,6 +38,20 @@ public class SseMessageUtils {
|
||||
MANAGER.sendMessage(userId, message);
|
||||
}
|
||||
|
||||
/**
|
||||
* 向指定的用户的指定会话发送消息
|
||||
*
|
||||
* @param userId 要发送消息的用户id
|
||||
* @param token 用户的会话令牌
|
||||
* @param message 要发送的消息内容
|
||||
*/
|
||||
public static void sendMessage(Long userId, String token, String message) {
|
||||
if (!isEnable()) {
|
||||
return;
|
||||
}
|
||||
MANAGER.sendMessage(userId, token, message);
|
||||
}
|
||||
|
||||
/**
|
||||
* 本机全用户会话发送消息
|
||||
*
|
||||
|
Reference in New Issue
Block a user