HTML5 EventSource的用法

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<h1>获取服务端更新数据</h1>
<div id="result"></div>

<script type="text/javascript">
    if (typeof (EventSource) !== "undefined") {
        var source = new EventSource("data.aspx");
        source.onmessage = function (event) {
           
            document.getElementById("result").innerHTML += event.data + "<br>";
        };
        source.onerror = function (event) {
            console.log("error"+event);
        }
        source.onopen = function () {
            console.log('开始连接');
        }
    }
    else {
        document.getElementById("result").innerHTML = "抱歉,你的浏览器不支持 server-sent 事件...";
    }
</script>

</body>
</html>
原文地址:https://www.cnblogs.com/tiancai/p/5834169.html