Springboot 实现SSE 消息推送(十七)

Controller

/**
 * @author : 徐长城
 * @des:
 * @date : 2019/9/18 22:13
 */
@RestController
@RequestMapping("/sse")
public class SSEController {

    @RequestMapping(value = "/get",produces = "text/event-stream;charset=UTF-8")
    public String getData(){
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "data:行情:" + Math.random()+"\n\n";
    }
}

注意:要加上"\n\n"不然没有数据

html页面




    
    Title
    


用户一

访问页面

Springboot 实现SSE 消息推送(十七)_第1张图片

Springboot 实现SSE 消息推送(十七)_第2张图片

但是有个很大的缺点就是一直在请求

Springboot 实现SSE 消息推送(十七)_第3张图片

大家也可以看下WebSocket的实现

https://blog.csdn.net/xcc_2269861428/article/details/100999810

你可能感兴趣的:(springboot,Springboot实现SSE)