分类

链接

2025 年 3 月
 12
3456789
10111213141516
17181920212223
24252627282930
31  

近期文章

热门标签

新人福利,免费薅羊毛

现在位置:    首页 > JAVA > 正文
共享办公室出租
去掉setInterval轮询,使用EventSource+SSE
JAVA 暂无评论 阅读(20)

去掉setInterval轮询,使用EventSource+SSE。核心 代码不多。

 

springboot创建SSE API

@RestController
@RequestMapping("/h5/sse")
@Log4j2
public class SseController {

    @Autowired
    SseService sseService;


    @GetMapping("/order/{orderId}")
    public SseEmitter handleSse(@PathVariable Long orderId) {

        return sseService.subscribeOrderStatus(orderId);
    }
}

 

创建SseService

public interface SseService {

    SseEmitter subscribeOrderStatus(Long orderId);

    void pushOrderStatusUpdate(Long orderId, Integer status);

    void removeOrderEmitter(Long orderId);
}

SSEServiceImpl:

@Service
@Log4j2
public class SseServiceImpl implements SseService {

    private final ConcurrentHashMap<Long, SseEmitter> orderEmitters = new ConcurrentHashMap<>();

    @Override
    public SseEmitter subscribeOrderStatus(Long orderId) {
        SseEmitter emitter = new SseEmitter(5 * 60 * 1000L);

        emitter.onTimeout(() -> orderEmitters.remove(orderId));
        emitter.onCompletion(() -> orderEmitters.remove(orderId));

        orderEmitters.put(orderId, emitter);

        return emitter;

    }

    // 推送订单状态更新
    @Override
    public void pushOrderStatusUpdate(Long orderId, Integer status) {
        SseEmitter emitter = orderEmitters.get(orderId);
        if (emitter != null) {
            try {
                Map<String, Object> data=new HashMap<>();
                data.put("orderId", orderId);
                data.put("status", status);

                emitter.send(JSONUtil.toString(data), MediaType.TEXT_EVENT_STREAM);
            } catch (Exception e) {
                emitter.completeWithError(e);
            }
        }
    }

    @Override
    public void removeOrderEmitter(Long orderId) {
        orderEmitters.remove(orderId);
    }
}

 

 

前端:

安装 event-source-polyfill (支持配置 headers)

function sse(id) {
  const eventSource = new EventSourcePolyfill(baseApiUrl + "/sse/order/" + id, {
    heartbeatTimeout: 5 * 60 * 1000, // 配置请求超时时间
    headers: {
      'Authorization': 'bearer ' + getToken(),
    }
  });

  eventSource.onmessage = function (event) {
    event = JSON.parse(event.data);

    console.log(event);
    if (event.status != reservation.value.status) {
       
      loadReservation();
    }
  };

  eventSource.onerror = function () {
    console.error("EventSource failed.");
  };
}

 

============ 欢迎各位老板打赏~ ===========

本文版权归Bruce's Blog所有,转载引用请完整注明以下信息:
本文作者:Bruce
本文地址:去掉setInterval轮询,使用EventSource+SSE | Bruce's Blog

发表评论

留言无头像?