lawrencehj
2021-06-22 3d88881f670c4404bf8c7c984a8f484a8f69f60f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package com.genersoft.iot.vmp.vmanager.gb28181.SseController;
 
import com.genersoft.iot.vmp.gb28181.event.alarm.AlarmEventListener;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
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
 */
@Api(tags = "SSE推送")
@CrossOrigin
@Controller
@RequestMapping("/api")
public class SseController {
    @Autowired
    AlarmEventListener alarmEventListener;
 
    @ApiOperation("浏览器推送")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "browserId", value = "浏览器ID", dataTypeClass = String.class),
    })
    @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;
    }
}