简介:SSE( Server-sent Events )是 WebSocket 的一种轻量代替方案,使用 HTTP 协议
SSE 是单向通道,只能服务器向客户端发送消息。
本文参考文档
https://blog.csdn.net/mybook201314/article/details/108661938
https://github.com/tianwyam/spring-boot-learn/tree/master/spring-boot-learn-capture-screen
在上述参考文档的基础上,
修改了spring-boot-learn-capture-screen功能里SSEController的sse方法,修改如下,实现屏幕截屏频率的定制和调整:
@GetMapping("/index")
public SseEmitter sse() {
// 获取屏幕截图,每个1秒获取一次,返回前端界面进行图片展示
// String capture = CaptureScreenUtils.capture();
// SseEmitter emitter = new SseEmitter(1L) ;
// try {
// emitter.send(SseEmitter.event()
// .reconnectTime(1)
// .data(capture));
// } catch (IOException e) {
// LOGGER.error("服务端推送服务发生异常", e);
// }
//
// return emitter;
SseEmitter emitter = new SseEmitter();
ExecutorService sseMvcExecutor = Executors.newSingleThreadExecutor();
sseMvcExecutor.execute(() -> {
try {
for (int i = 0; true; i++) {
String capture = CaptureScreenUtils.capture();
emitter.send(SseEmitter.event()
.reconnectTime(1)
.data(capture));
Thread.sleep(1000);
}
} catch (Exception ex) {
emitter.completeWithError(ex);
}
});
return emitter;
}