-
Notifications
You must be signed in to change notification settings - Fork 1
/
websocket_demo.html
165 lines (131 loc) · 3.26 KB
/
websocket_demo.html
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<html>
<head>
<title>WebSockets Demo</title>
<meta charset = "utf-8" />
<style>
body {
font-family: 'Liberation Sans';
font-size: 22px;
}
</style>
<script language = "javascript" type = "text/javascript">
var websock = new WebSocket("ws://localhost:1975/");
var ignore_websock = false;
websock.onopen = function() {
var freq = document.getElementById("spin");
console.log("Connected");
send_msg(freq.value);
};
websock.onclose = function() {
console.log("Connection closed");
websock.close();
};
websock.onerror = function() {
console.log("Error");
};
websock.onmessage = function(m) {
var msg = JSON.parse(m.data);
var dropdown = "<select id = \"netif\" onmouseenter = \"ign_ws()\" onmouseleave =\"unign_ws()\">";
var i;
var select;
var selected_if = false;
if (ignore_websock)
return;
console.log(m.data);
document.getElementById("host").innerHTML = msg.host;
document.getElementById("peerip").innerHTML = msg.peerip;
document.getElementById("uptime").innerHTML = msg.uptime;
document.getElementById("rxb").innerHTML = msg.rx;
document.getElementById("txb").innerHTML = msg.tx;
for (i = 0; i < msg.ifnames.length; i++) {
var netif = msg.ifnames[i];
select = "";
if (netif == msg.ifname) {
selected_if = true;
select = "selected";
}
dropdown += "<option value =\"" + netif +
"\" onclick = \"select_netif()\" " + select + ">" +
netif + "</option>";
}
dropdown += "</select>";
document.getElementById("netif_dd").innerHTML = dropdown;
/*
* If the interface went away, fall back to the first interface
* in the list.
*/
if (!selected_if)
websock.send(msg.ifnames[0]);
};
function select_netif() {
var e = document.getElementById("netif");
websock.send(e.options[e.selectedIndex].value);
ignore_websock = false;
};
function ign_ws() {
ignore_websock = true;
};
function unign_ws() {
ignore_websock = false;
};
function send_msg(freq) {
websock.send(freq);
};
function spinner(direction) {
var freq = document.getElementById("spin");
if (direction == "inc")
freq.value++;
else if (direction == "dec" && freq.value > 0)
freq.value--;
else
return;
send_msg(freq.value);
};
window.onbeforeunload = function() {
websock.close();
};
</script>
</head>
<body>
<center>
<big>WebSockets Demo</big>
<p />
<big><b><span id = "host"></span></b></big>
<p />
<font color = "#666666" size = "4">your IP address: <span id = "peerip"></span></font>
</center>
<br /><br />
<center>
<table>
<tbody>
<tr>
<td align = "right"><b>Uptime : </b></td>
<td align = "left"><span id = "uptime"></span></td>
</tr>
<tr>
<td align = "right"><b>IFace : </b></td>
<td align = "left"><span id = "netif_dd"></span></td>
</tr>
<tr>
<td align = "right"><b>RX bytes : </b></td>
<td align = "left"><span id = "rxb"></span></td>
</tr>
<tr>
<td align = "right"><b>TX bytes : </b></td>
<td align = "left"><span id = "txb"></span></td>
</tr>
</tbody>
</table>
</center>
<p />
<center>
<small>Set update frequency <small>(seconds)</small></small>
<p />
<input type = "text" id = "spin" size = "2" value = "2" readonly />
<button type = "button" onclick = "spinner('inc');">⬆</button>
<button type = "button" onclick = "spinner('dec');">⬇</button>
<br />
<small><small>0 disables updates</small></small>
</center>
</body>
</html>