问题:做可视化大屏,开始有数据,刷新几次页面之后就没数据了很奇怪,前端的请求在不停的重新连接。
解决方案:因为之前的代码中使用了单个 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;
im