王兵 4 years ago
parent 1196f301e8
commit f40c8112a8

@ -0,0 +1,58 @@
package xyz.wbsite.frame.sse;
import org.springframework.http.MediaType;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
import xyz.wbsite.frame.utils.MapperUtil;
import java.io.IOException;
import java.util.concurrent.ConcurrentHashMap;
public class Sser {
private static ConcurrentHashMap<String, SseEmitter> sseMap = new ConcurrentHashMap();
/**
*
*/
public SseEmitter register(String key, SseEmitter sseEmitter) {
if (sseMap.get(key) != null) {
sseMap.remove(key);
}
sseMap.put(key, sseEmitter);
return sseEmitter;
}
/**
*
*
* @param key key
* @param data
*/
public static void push(String key, Object data) {
SseEmitter sseEmitter = sseMap.get(key);
if (sseEmitter == null) {
return;
}
try {
sseEmitter.send(MapperUtil.toJson(data), MediaType.APPLICATION_JSON);
} catch (IOException e) {
sseMap.remove(key);
}
}
/**
*
*
* @param data
*/
public static void pushAll(Object data) {
for (String s : sseMap.keySet()) {
try {
sseMap.get(s).send(MapperUtil.toJson(data), MediaType.APPLICATION_JSON);
} catch (IOException e) {
sseMap.remove(s);
}
}
}
}
Loading…
Cancel
Save

Powered by TurnKey Linux.