websocket.html.j2
3.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<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://172.27.1.139: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>
<object data="http://{{ ip }}:{{ port }}/ldap" type="image/svg+xml" class="mailicon">
</object>
</body>
</html>
<!-- vim: set ts=4 sw=4: -->