Java Server-Sent Events没有数据

文章讲述了在实现可视化大屏功能时遇到的问题,即数据刷新后消失,原因是单个SseEmitter实例未正确关闭。作者提供了解决方案,建议为每个请求创建新的SseEmitter实例,并确保清理旧连接,以避免连接冲突和提高性能。
摘要由CSDN通过智能技术生成

问题:做可视化大屏,开始有数据,刷新几次页面之后就没数据了很奇怪,前端的请求在不停的重新连接。

解决方案:因为之前的代码中使用了单个 SseEmitter 实例,这导致旧的连接没有正确关闭而产生冲突。可以通过为每个请求创建一个新的 SseEmitter 实例来优化代码,同时确保旧的实例被适当地清理。

示例:

import com.alibaba.fastjson.JSONObject;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
import xin.admin.domain.sse.NotificationSSE;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@RestController
@RequestMapping("/admin/homePage")
public class NotificationSSEController {

    // 线程池,让sse异步操作
    private final ExecutorService executorService = Executors.newCachedThreadPool();

    // 使用Map来存储每个客户端的SseEmitter
    private final Map<String, SseEmitter> emitters = new ConcurrentHashMap<>();

    // 要发送的数据
    public static NotificationSSE data = new NotificationSSE();

    @CrossOrigin
    @RequestMapping(value = "/notification", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public SseEmitter stream(HttpServletRequest request) {
        String clientId = getClientId(request);
        SseEmitter emitter = new SseEmitter(24L * 60 * 60 * 1000);

        emitters.put(clientId, emitter);

        emitter.onCompletion(() -> emitters.remove(clientId));
        emitter.onTimeout(() -> emitters.remove(clientId));
        emitter.onError((e) -> emitters.remove(clientId));

        sendNotification(); // 当客户端连接时立即发送通知
        return emitter;
    }

    @Scheduled(fixedRate = 1000 * 60 * 10)
    public void heartbeat() {
        sendNotification();
    }

    // B函数:负责SSE发送
    public void sendNotification() {
        emitters.forEach((clientId, emitter) -> {
            executorService.execute(() -> {
                try {
                    emitter.send(SseEmitter.event()
                            .id(String.valueOf(System.currentTimeMillis()))
                            .data(JSONObject.toJSONString(data)));
                } catch (Exception e) {
                    emitter.completeWithError(e);
                }
            });
        });
    }

    // 生成或获取客户端ID
    private String getClientId(HttpServletRequest request) {
        // 这里可以根据实际情况生成唯一的客户端ID
        return request.getSession(true).getId();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值