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>

 

posted @ 2016-09-02 17:07  甜菜波波  阅读(2674)  评论(0编辑  收藏  举报