You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

37 lines
1.1 KiB
HTML

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<h1>EventSource获取服务端更新数据</h1>
<div id="result"></div>
<script type="text/javascript">
if (typeof (EventSource) !== "undefined") {
debugger
//http://192.168.6.235:82/stage-api/biz/api/data/getMsg
//http://192.168.6.235:82/stage-api/biz/api/data/get_data
var source = new EventSource("http://192.168.6.235:82/stage-api/biz/api/data/get_data");//,{ withCredentials: true }跨域
source.onmessage = function (event) {
//debugger
document.getElementById("result").innerHTML += event.data + "<br>";
};
source.onerror = function (event) {
//debugger
console.log("error:" + JSON.stringify(event) + JSON.stringify(source));
}
source.onopen = function (event) {
//console.log('开始连接');
//debugger
console.log("open:" + JSON.stringify(event));
}
}
else {
document.getElementById("result").innerHTML = "抱歉,你的浏览器不支持 server-sent 事件...";
}
</script>
</body>
</html>