fix(报警推送): 修复报警推送功能无效的问题
该问题原因为 com.genersoft.iot.vmp.conf.GlobalResponseAdvice 类改变了 sse 响应体的数据结构,导致前端无法正确解析 sse 数据,调试后未发现 GlobalResponseAdvice 如何修改的 sse 数据结构,故根据 sse 消息体结构自定义实现了 sse 连接
This commit is contained in:
@@ -1,37 +0,0 @@
|
||||
package com.genersoft.iot.vmp.vmanager.gb28181.SseController;
|
||||
|
||||
import com.genersoft.iot.vmp.gb28181.event.alarm.AlarmEventListener;
|
||||
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.CrossOrigin;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
|
||||
|
||||
/**
|
||||
* @description: SSE推送
|
||||
* @author: lawrencehj
|
||||
* @data: 2021-01-20
|
||||
*/
|
||||
@Tag(name = "SSE推送")
|
||||
|
||||
@Controller
|
||||
@RequestMapping("/api")
|
||||
public class SseController {
|
||||
@Autowired
|
||||
AlarmEventListener alarmEventListener;
|
||||
|
||||
@GetMapping("/emit")
|
||||
public SseEmitter emit(@RequestParam String browserId) {
|
||||
final SseEmitter sseEmitter = new SseEmitter(0L);
|
||||
try {
|
||||
alarmEventListener.addSseEmitters(browserId, sseEmitter);
|
||||
}catch (Exception e){
|
||||
sseEmitter.completeWithError(e);
|
||||
}
|
||||
return sseEmitter;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package com.genersoft.iot.vmp.vmanager.gb28181.sse;
|
||||
|
||||
import com.genersoft.iot.vmp.gb28181.event.alarm.AlarmEventListener;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
|
||||
|
||||
/**
|
||||
* SSE 推送.
|
||||
*
|
||||
* @author lawrencehj
|
||||
* @author <a href="mailto:xiaoQQya@126.com">xiaoQQya</a>
|
||||
* @since 2021/01/20
|
||||
*/
|
||||
@Tag(name = "SSE 推送")
|
||||
@RestController
|
||||
@RequestMapping("/api")
|
||||
public class SseController {
|
||||
|
||||
@Resource
|
||||
private AlarmEventListener alarmEventListener;
|
||||
|
||||
/**
|
||||
* SSE 推送.
|
||||
*
|
||||
* @param response 响应
|
||||
* @param browserId 浏览器ID
|
||||
* @throws IOException IOEXCEPTION
|
||||
* @author <a href="mailto:xiaoQQya@126.com">xiaoQQya</a>
|
||||
* @since 2023/11/06
|
||||
*/
|
||||
@GetMapping("/emit")
|
||||
public void emit(HttpServletResponse response, @RequestParam String browserId) throws IOException, InterruptedException {
|
||||
response.setContentType("text/event-stream");
|
||||
response.setCharacterEncoding("utf-8");
|
||||
|
||||
PrintWriter writer = response.getWriter();
|
||||
alarmEventListener.addSseEmitter(browserId, writer);
|
||||
|
||||
while (!writer.checkError()) {
|
||||
Thread.sleep(1000);
|
||||
writer.write(":keep alive\n\n");
|
||||
writer.flush();
|
||||
}
|
||||
alarmEventListener.removeSseEmitter(browserId, writer);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user