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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
|
<!DOCTYPE html>
<html>
<head>
<meta charset='UTF-8'>
<title>Websocket Meters</title>
<!--
Version 0.1 Contributed by William Greathouse 9-Sep-2013
Simple demo of WebSocket connection use. Not a great example of web coding,
but it is functional.
The meter displays are adapted from CSS-TRICKS Progress Bars by Chris Coyier
at http://css-tricks.com/css3-progress-bars/
-->
<style>
body {
background: #222;
}
h1 {
color: white;
text-align: center;
}
button {
width: 225px;
height: 30px;
margin: auto 10px;
background-color: #ccc;
-moz-border-radius: 5px;
-webkit-border-radius: 5px;
border-radius:6px;
color: blue;
font-size: 20px;
}
button:hover {
background-color: #888;
}
button:hover:disabled {
background-color: #ccc;
}
button:disabled {
color: lightgray;
}
.button_container {
width:550px;
display:block;
margin-left:auto;
margin-right:auto;
}
.meter {
height: 20px; /* Can be anything */
position: relative;
background: #555;
-moz-border-radius: 25px;
-webkit-border-radius: 25px;
border-radius: 25px;
padding: 10px;
-webkit-box-shadow: inset 0 -1px 1px rgba(255,255,255,0.3);
-moz-box-shadow : inset 0 -1px 1px rgba(255,255,255,0.3);
box-shadow : inset 0 -1px 1px rgba(255,255,255,0.3);
}
.meter > span {
display: block;
height: 100%;
-webkit-border-top-right-radius: 20px;
-webkit-border-bottom-right-radius: 20px;
-moz-border-radius-topright: 20px;
-moz-border-radius-bottomright: 20px;
border-top-right-radius: 20px;
border-bottom-right-radius: 20px;
-webkit-border-top-left-radius: 20px;
-webkit-border-bottom-left-radius: 20px;
-moz-border-radius-topleft: 20px;
-moz-border-radius-bottomleft: 20px;
border-top-left-radius: 20px;
border-bottom-left-radius: 20px;
background-color: rgb(43,194,83);
background-image: -webkit-gradient(
linear,
left bottom,
left top,
color-stop(0, rgb(43,194,83)),
color-stop(1, rgb(84,240,84))
);
background-image: -moz-linear-gradient(
center bottom,
rgb(43,194,83) 37%,
rgb(84,240,84) 69%
);
-webkit-box-shadow:
inset 0 2px 9px rgba(255,255,255,0.3),
inset 0 -2px 6px rgba(0,0,0,0.4);
-moz-box-shadow:
inset 0 2px 9px rgba(255,255,255,0.3),
inset 0 -2px 6px rgba(0,0,0,0.4);
box-shadow:
inset 0 2px 9px rgba(255,255,255,0.3),
inset 0 -2px 6px rgba(0,0,0,0.4);
position: relative;
overflow: hidden;
}
.meter > span:after, .animate > span > span {
content: "";
position: absolute;
top: 0; left: 0; bottom: 0; right: 0;
background-image:
-webkit-gradient(linear, 0 0, 100% 100%,
color-stop(.25, rgba(255, 255, 255, .2)),
color-stop(.25, transparent), color-stop(.5, transparent),
color-stop(.5, rgba(255, 255, 255, .2)),
color-stop(.75, rgba(255, 255, 255, .2)),
color-stop(.75, transparent), to(transparent)
);
background-image:
-moz-linear-gradient(
-45deg,
rgba(255, 255, 255, .2) 25%,
transparent 25%,
transparent 50%,
rgba(255, 255, 255, .2) 50%,
rgba(255, 255, 255, .2) 75%,
transparent 75%,
transparent
);
z-index: 1;
-webkit-background-size: 50px 50px;
-moz-background-size: 50px 50px;
-webkit-animation: move 2s linear infinite;
-webkit-border-top-right-radius: 20px;
-webkit-border-bottom-right-radius: 20px;
-moz-border-radius-topright: 20px;
-moz-border-radius-bottomright: 20px;
border-top-right-radius: 20px;
border-bottom-right-radius: 20px;
-webkit-border-top-left-radius: 20px;
-webkit-border-bottom-left-radius: 20px;
-moz-border-radius-topleft: 20px;
-moz-border-radius-bottomleft: 20px;
border-top-left-radius: 20px;
border-bottom-left-radius: 20px;
overflow: hidden;
}
.animate > span:after {
display: none;
}
@-webkit-keyframes move {
0% {
background-position: 0 0;
}
100% {
background-position: 50px 50px;
}
}
.orange > span {
background-color: #f1a165;
background-image: -moz-linear-gradient(top, #f1a165, #f36d0a);
background-image: -webkit-gradient(linear,left top,left bottom,color-stop(0, #f1a165),color-stop(1, #f36d0a));
background-image: -webkit-linear-gradient(#f1a165, #f36d0a);
}
.red > span {
background-color: #f0a3a3;
background-image: -moz-linear-gradient(top, #f0a3a3, #f42323);
background-image: -webkit-gradient(linear,left top,left bottom,color-stop(0, #f0a3a3),color-stop(1, #f42323));
background-image: -webkit-linear-gradient(#f0a3a3, #f42323);
}
.nostripes > span > span, .nostripes > span:after {
-webkit-animation: none;
background-image: none;
}
#output {
background-color: #ccc;
height: 240px;
overflow-y: auto;
}
</style>
</head>
<body>
<div id="page_wrap">
<h1>Meter Updates via WebSocket</h1>
<p/>
<div class="meter">
<span id="meter1" style="width: 25%"></span>
</div>
<p/>
<div class="meter orange nostripes">
<span id="meter2" style="width: 33.3%"></span>
</div>
<p/>
<div class="meter red">
<span id="meter3" style="width: 80%"></span>
</div>
<p/>
</div>
<div class="button_container">
<div>
<button id="connection" onclick="toggleConnection(this)">WebSocket Connect</button>
<button id="update" disabled onclick="toggleUpdate(this)">Disable Update</button>
</div>
</div>
<p/>
<div id="output"></div>
</body>
<script language="javascript" type="text/javascript">
var connection; // websocket connection
function writeToScreen (message) {
var div = document.createElement('div');
var output = document.getElementById('output');
div.innerHTML = message;
output.appendChild(div);
output.scrollTop = output.scrollHeight;
}
function ws_connect() {
// check for websocket support
// for Internet Explorer < 10 there are options for websocket support that
// could be integrated into production code, but for now, we are expecting
// browser support to be present for this demo
if ('WebSocket' in window) {
writeToScreen('Connecting');
connection = new WebSocket('ws://' + window.location.host + '/meters');
connection.onopen = function(ev) {
document.getElementById("connection").innerHTML = "WebSocket Disconnect";
document.getElementById("update").disabled=false;
document.getElementById("update").innerHTML = "Disable Update";
writeToScreen('CONNECTED');
var message = 'update on';
writeToScreen('SENT: ' + message);
connection.send(message);
};
connection.onclose = function(ev) {
document.getElementById("update").disabled=true;
document.getElementById("update").innerHTML = "Enable Update";
document.getElementById("connection").innerHTML = "WebSocket Connect";
writeToScreen('DISCONNECTED');
};
connection.onmessage = function(ev) {
if (ev.data.substr(0,5) == "meter")
{
var target = ev.data.split(":")[0];
var meter = document.getElementById(target);
var data = ev.data.split(":")[1].split(",");
var percent = (data[0]*100)/data[1];
meter.style.width = percent+"%";
}
else
writeToScreen('RECEIVED: ' + ev.data);
};
connection.onerror = function(ev) {
alert("WebSocket error");
};
} else {
alert("WebSocket is not available!!!\n" +
"Demo will not function.");
}
}
// user connect/disconnect
function toggleConnection(el) {
var tag=el.innerHTML;
if (tag == "WebSocket Connect")
{
ws_connect();
}
else
{
connection.close();
}
}
// user turn updates on/off
function toggleUpdate(el) {
var tag=el.innerHTML;
var message;
if (tag == "Enable Update")
{
message = 'update on';
el.innerHTML = "Disable Update";
}
else
{
message = 'update off';
el.innerHTML = "Enable Update";
}
writeToScreen('SENT: ' + message);
connection.send(message);
}
</script>
</html>
|