Html程序  |  55行  |  1.54 KB

<!doctype html>
<html>
<head>
<title>EventSource Loader Buffering</title>
<script>
window.onload = function () {
    setTimeout(test_es, 500);
};

function test_es() {
    var len = 0;
    var count = 0;
    var step = 1000;
    var es = new EventSource("eventsource-loader-buffering.php");
    es.onopen = function () {
        log("got 'open' event");
        log("waiting for the first " + step + " 'message' events");
    };
    es.onmessage = function (evt) {
        len += evt.data.length;
        if (++count % step)
            return;
        log("got " + count + " 'message' events (" + len + " characters)", 1);
        if (len >= 1e9)
            end();
    };
    es.onerror = function () {
        log("got 'error' event");
        end();
    };
    function end() {
        if (es.readyState != es.CLOSED)
            es.close();
        log("ENDED");
    };
}

function log(message, updateLast) {
    if (!log.list)
        log.list = document.getElementById("log");
    var text = document.createTextNode(message);
    if (updateLast)
        log.list.lastChild.replaceChild(text, log.list.lastChild.firstChild);
    else
        log.list.appendChild(document.createElement("li")).appendChild(text);
}
</script>
</head>
<body>
<p>Manual test to verify that the EventSource loader does not buffer data (may result in memory growth with long lived connections). Monitor memory usage; it should be stable. This file has to be served from the same web server as the php script with the same name.</p>
<ul id="log" style="list-style-type: none"></ul>
</body>
</html>