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>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM