<meta charset="UTF-8"> 
<html>
	<body>
		<img src="libwebsockets.org-logo.png"><br>
	
		Hello from the <b>minimal http Server Side Events + Ring example</b>.
		<p>
		This is a static page served from ./mount-origin/index.html.
		<p>
		It connects back to the server at <i>/sse/sourcename</i> using EventSource()<br>
		and displays the perioding incoming event data below.
		<p>
		The data is being produced by two asynchronous threads at the server,
		which each sleep for a random period inbetween samples.
		<p>
		<textarea id=r readonly cols=60 rows=20></textarea><br>
	</body>
<script>

var head = 0, tail = 0, ring = new Array();

es = new EventSource("/sse/sourcename");
try {
	es.onopen = function() {
		// console.log("EventSource opened");
		document.getElementById("r").disabled = 0;
	} 

	es.onmessage = function got_packet(msg) {
		var n, s = "";

		// console.log(msg.data);
		ring[head] = msg.data + "\n";
		head = (head + 1) % 50;
		if (tail == head)
			tail = (tail + 1) % 50;

		n = tail;
		do {
			s = s + ring[n];
			n = (n + 1) % 50;
		} while (n != head);

		document.getElementById("r").value = s; 
		document.getElementById("r").scrollTop =
			document.getElementById("r").scrollHeight;
	} 

	/* there is no onclose() for EventSource */

} catch(exception) {
	alert('<p>Error' + exception);  
}

</script>

</html>