1document.addEventListener("DOMContentLoaded", function() { 2 3 var head = 0, tail = 0, ring = new Array(), es; 4 5 es = new EventSource("/sse/sourcename"); 6 try { 7 es.onopen = function() { 8 // console.log("EventSource opened"); 9 document.getElementById("r").disabled = 0; 10 }; 11 12 es.onmessage = function got_packet(msg) { 13 var n, s = ""; 14 15 // console.log(msg.data); 16 ring[head] = msg.data + "\n"; 17 head = (head + 1) % 50; 18 if (tail === head) 19 tail = (tail + 1) % 50; 20 21 n = tail; 22 do { 23 s = s + ring[n]; 24 n = (n + 1) % 50; 25 } while (n !== head); 26 27 document.getElementById("r").value = s; 28 document.getElementById("r").scrollTop = 29 document.getElementById("r").scrollHeight; 30 }; 31 32 /* there is no onclose() for EventSource */ 33 34 } catch(exception) { 35 alert("<p>Error " + exception); 36 } 37 38}, false); 39