一:前端
<!DOCTYPE html>
<html>
<head></head>
<script type="text/javascript">
function uu(){
if(typeof(EventSource)!=="undefined") { //判断是否支持EventSource
var source = new EventSource('http://127.0.0.1:8060/text/terInfo1');
source.addEventListener('message', function (e) {
console.log(e.data);
});
source.addEventListener('open', function (e) {
console.log(e.data);
});
source.addEventListener('error', function (e) {
console.log(e.data);
});
}else{
console.log("e.data");
}
}
uu()
</script>
<body>
</body>
</html>
二:后台
@RestController @RequestMapping("/text") public class text { @RequestMapping(value = "/getHello", produces = "text/event-stream;charset=UTF-8") @ResponseBody public String index() { try { Thread.sleep(1000); //第三方数据源调用 } catch (InterruptedException e) { e.printStackTrace(); } return "data:返回数据" + Math.random() + "\n\n"; } }