1234567891011121314151617181920212223242526272829303132333435363738 |
- package com.rf.AIquantum.utils;
- /**
- * @Author:zzf
- * @Date:2025/3/10:18:25
- * @Description:
- */
- import org.springframework.stereotype.Component;
- import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
- import java.util.Map;
- import java.util.concurrent.ConcurrentHashMap;
- @Component
- public class SseEmitterService {
- private final Map<String, SseEmitter> emitters = new ConcurrentHashMap<>();
- public void addEmitter(String clientId, SseEmitter emitter) {
- emitters.put(clientId, emitter);
- }
- public void removeEmitter(String clientId) {
- emitters.remove(clientId);
- }
- public void sendMessage(String clientId, Object message) {
- SseEmitter emitter = emitters.get(clientId);
- if (emitter != null) {
- try {
- emitter.send(message);
- } catch (Exception e) {
- // 处理异常,如网络问题等
- emitters.remove(clientId);
- }
- }
- }
- }
|