SpringBoot2.x服务端主动推送SSE

讲解SpringBoot2.x服务端主动推送Sever-Send-Events
        
    1、localhost:8080/index.html
    2、需要把response的类型 改为 text/event-stream,才是sse的类型

 

   调用的controller

   

@RestController
@RequestMapping("/sse")
public class SSEController {

    @RequestMapping(value = "/get_data", produces = "text/event-stream;charset=UTF-8")
    public String push() {
          
          try {
              Thread.sleep(1000); 
              //第三方数据源调用
          } catch (InterruptedException e) {
              e.printStackTrace();
          }

          return "data:xdclass 行情" + Math.random() + "\n\n";
    }
}

 

对应的index.html页面





Insert title here
 


模拟股票行情
 

xdclass test
 
   
 


你可能感兴趣的:(Springboot2.x)