websocket.html 2.98 KB
<html>
	<head>
		<title>Websocket test</title>
	</head>

	<body>
		<h1>Websocket Test</h1>
		<div id="wstest">Websockets are not supported</div>
		<div id="data">Server time</div>

		<script language="Javascript">
		/* <![CDATA[ */
		var wstest = document.getElementById('wstest').firstChild;
		var data   = document.getElementById('data');

		var current_time = null;
		var recv_date    = null;
		var recv_at      = null;

		function isLittleEndian() {
			var a = new ArrayBuffer(4);
			var b = new Uint8Array(a);
			var c = new Uint32Array(a);
			b[0] = 0xa1;
			b[1] = 0xb2;
			b[2] = 0xc3;
			b[3] = 0xd4;
			return c[0] == 0xd4c3b2a1;
		}

		function swap_first(view) {
			var tmp = view[0];
			view[0] = view[1];
			view[1] = tmp;
		}

		function ntohs(byteBuffer) {
			if (isLittleEndian()) {
				swap_first(byteBuffer);
			}
		}

		function ntohl(shortBuffer) {
			if (isLittleEndian()) {
				swap_first(shortBuffer);
				swap_first(new Uint8Array(shortBuffer.buffer, 0, 2));
				swap_first(new Uint8Array(shortBuffer.buffer, 2, 2));
			}
		}

		function ntohll(longBuffer) {
			if (isLittleEndian()) {
				swap_first(longBuffer);
				swap_first(new Uint16Array(longBuffer.buffer, 0, 2));
				swap_first(new Uint16Array(longBuffer.buffer, 4, 2));
				swap_first(new Uint8Array(longBuffer.buffer, 0, 2));
				swap_first(new Uint8Array(longBuffer.buffer, 2, 2));
				swap_first(new Uint8Array(longBuffer.buffer, 4, 2));
				swap_first(new Uint8Array(longBuffer.buffer, 6, 2));
			}
		}

		function updateData() {
			while (data.lastChild) {
				data.removeChild(data.lastChild);
			}
			data.appendChild(
				document.createTextNode(current_time.toISOString()));
			data.appendChild(
				document.createElement('br'));
			data.appendChild(
				document.createTextNode(current_time.toUTCString()));
			data.appendChild(
				document.createElement('br'));
			data.appendChild(
				document.createTextNode(current_time.toLocaleString()));
		}

		if ('WebSocket' in window){
			var interval = null

			wstest.data = 'Websockets are supported';
			connection = new WebSocket('ws://127.0.0.1:8080/');
			console.log('Websockets test');

			connection.onopen = function() {
				interval = setInterval(
						function() {
							if(current_time) {
								current_time = new Date(recv_date.getTime() + (new Date().getTime() - recv_at));
								updateData();
							}
						}, 1);
			}

			connection.onclose = function() {
				window.clearInterval(interval)
			}

			connection.onmessage = function(e){
				var reader = new FileReader();
				reader.addEventListener("loadend", function() {
						ntohll(new Uint32Array(reader.result, 0, 2))
						current_time = recv_date = new Date(Math.round(
								new Float64Array(reader.result)[0] * 1000))
						recv_at = new Date().getTime();
					});
				reader.readAsArrayBuffer(e.data);
			}

			connection.onerror = function(e){
				window.clearInterval(interval)
				console.log(e.data);
			}
		}/* ]]> */
		</script>
		<img src="http://127.0.0.1:8080/ldap" />
	</body>
</html>
<!-- vim: set ts=4 sw=4: -->