index.html 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset='UTF-8'>
  5. <title>Websocket Meters</title>
  6. <!--
  7. Version 0.1 Contributed by William Greathouse 9-Sep-2013
  8. Simple demo of WebSocket connection use. Not a great example of web coding,
  9. but it is functional.
  10. The meter displays are adapted from CSS-TRICKS Progress Bars by Chris Coyier
  11. at http://css-tricks.com/css3-progress-bars/
  12. -->
  13. <style>
  14. body {
  15. background: #222;
  16. }
  17. h1 {
  18. color: white;
  19. text-align: center;
  20. }
  21. button {
  22. width: 225px;
  23. height: 30px;
  24. margin: auto 10px;
  25. background-color: #ccc;
  26. -moz-border-radius: 5px;
  27. -webkit-border-radius: 5px;
  28. border-radius:6px;
  29. color: blue;
  30. font-size: 20px;
  31. }
  32. button:hover {
  33. background-color: #888;
  34. }
  35. button:hover:disabled {
  36. background-color: #ccc;
  37. }
  38. button:disabled {
  39. color: lightgray;
  40. }
  41. .button_container {
  42. width:550px;
  43. display:block;
  44. margin-left:auto;
  45. margin-right:auto;
  46. }
  47. .meter {
  48. height: 20px; /* Can be anything */
  49. position: relative;
  50. background: #555;
  51. -moz-border-radius: 25px;
  52. -webkit-border-radius: 25px;
  53. border-radius: 25px;
  54. padding: 10px;
  55. -webkit-box-shadow: inset 0 -1px 1px rgba(255,255,255,0.3);
  56. -moz-box-shadow : inset 0 -1px 1px rgba(255,255,255,0.3);
  57. box-shadow : inset 0 -1px 1px rgba(255,255,255,0.3);
  58. }
  59. .meter > span {
  60. display: block;
  61. height: 100%;
  62. -webkit-border-top-right-radius: 20px;
  63. -webkit-border-bottom-right-radius: 20px;
  64. -moz-border-radius-topright: 20px;
  65. -moz-border-radius-bottomright: 20px;
  66. border-top-right-radius: 20px;
  67. border-bottom-right-radius: 20px;
  68. -webkit-border-top-left-radius: 20px;
  69. -webkit-border-bottom-left-radius: 20px;
  70. -moz-border-radius-topleft: 20px;
  71. -moz-border-radius-bottomleft: 20px;
  72. border-top-left-radius: 20px;
  73. border-bottom-left-radius: 20px;
  74. background-color: rgb(43,194,83);
  75. background-image: -webkit-gradient(
  76. linear,
  77. left bottom,
  78. left top,
  79. color-stop(0, rgb(43,194,83)),
  80. color-stop(1, rgb(84,240,84))
  81. );
  82. background-image: -moz-linear-gradient(
  83. center bottom,
  84. rgb(43,194,83) 37%,
  85. rgb(84,240,84) 69%
  86. );
  87. -webkit-box-shadow:
  88. inset 0 2px 9px rgba(255,255,255,0.3),
  89. inset 0 -2px 6px rgba(0,0,0,0.4);
  90. -moz-box-shadow:
  91. inset 0 2px 9px rgba(255,255,255,0.3),
  92. inset 0 -2px 6px rgba(0,0,0,0.4);
  93. box-shadow:
  94. inset 0 2px 9px rgba(255,255,255,0.3),
  95. inset 0 -2px 6px rgba(0,0,0,0.4);
  96. position: relative;
  97. overflow: hidden;
  98. }
  99. .meter > span:after, .animate > span > span {
  100. content: "";
  101. position: absolute;
  102. top: 0; left: 0; bottom: 0; right: 0;
  103. background-image:
  104. -webkit-gradient(linear, 0 0, 100% 100%,
  105. color-stop(.25, rgba(255, 255, 255, .2)),
  106. color-stop(.25, transparent), color-stop(.5, transparent),
  107. color-stop(.5, rgba(255, 255, 255, .2)),
  108. color-stop(.75, rgba(255, 255, 255, .2)),
  109. color-stop(.75, transparent), to(transparent)
  110. );
  111. background-image:
  112. -moz-linear-gradient(
  113. -45deg,
  114. rgba(255, 255, 255, .2) 25%,
  115. transparent 25%,
  116. transparent 50%,
  117. rgba(255, 255, 255, .2) 50%,
  118. rgba(255, 255, 255, .2) 75%,
  119. transparent 75%,
  120. transparent
  121. );
  122. z-index: 1;
  123. -webkit-background-size: 50px 50px;
  124. -moz-background-size: 50px 50px;
  125. -webkit-animation: move 2s linear infinite;
  126. -webkit-border-top-right-radius: 20px;
  127. -webkit-border-bottom-right-radius: 20px;
  128. -moz-border-radius-topright: 20px;
  129. -moz-border-radius-bottomright: 20px;
  130. border-top-right-radius: 20px;
  131. border-bottom-right-radius: 20px;
  132. -webkit-border-top-left-radius: 20px;
  133. -webkit-border-bottom-left-radius: 20px;
  134. -moz-border-radius-topleft: 20px;
  135. -moz-border-radius-bottomleft: 20px;
  136. border-top-left-radius: 20px;
  137. border-bottom-left-radius: 20px;
  138. overflow: hidden;
  139. }
  140. .animate > span:after {
  141. display: none;
  142. }
  143. @-webkit-keyframes move {
  144. 0% {
  145. background-position: 0 0;
  146. }
  147. 100% {
  148. background-position: 50px 50px;
  149. }
  150. }
  151. .orange > span {
  152. background-color: #f1a165;
  153. background-image: -moz-linear-gradient(top, #f1a165, #f36d0a);
  154. background-image: -webkit-gradient(linear,left top,left bottom,color-stop(0, #f1a165),color-stop(1, #f36d0a));
  155. background-image: -webkit-linear-gradient(#f1a165, #f36d0a);
  156. }
  157. .red > span {
  158. background-color: #f0a3a3;
  159. background-image: -moz-linear-gradient(top, #f0a3a3, #f42323);
  160. background-image: -webkit-gradient(linear,left top,left bottom,color-stop(0, #f0a3a3),color-stop(1, #f42323));
  161. background-image: -webkit-linear-gradient(#f0a3a3, #f42323);
  162. }
  163. .nostripes > span > span, .nostripes > span:after {
  164. -webkit-animation: none;
  165. background-image: none;
  166. }
  167. #output {
  168. background-color: #ccc;
  169. height: 240px;
  170. overflow-y: auto;
  171. }
  172. </style>
  173. </head>
  174. <body>
  175. <div id="page_wrap">
  176. <h1>Meter Updates via WebSocket</h1>
  177. <p/>
  178. <div class="meter">
  179. <span id="meter1" style="width: 25%"></span>
  180. </div>
  181. <p/>
  182. <div class="meter orange nostripes">
  183. <span id="meter2" style="width: 33.3%"></span>
  184. </div>
  185. <p/>
  186. <div class="meter red">
  187. <span id="meter3" style="width: 80%"></span>
  188. </div>
  189. <p/>
  190. </div>
  191. <div class="button_container">
  192. <div>
  193. <button id="connection" onclick="toggleConnection(this)">WebSocket Connect</button>
  194. <button id="update" disabled onclick="toggleUpdate(this)">Disable Update</button>
  195. </div>
  196. </div>
  197. <p/>
  198. <div id="output"></div>
  199. </body>
  200. <script language="javascript" type="text/javascript">
  201. var connection; // websocket connection
  202. function writeToScreen (message) {
  203. var div = document.createElement('div');
  204. var output = document.getElementById('output');
  205. div.innerHTML = message;
  206. output.appendChild(div);
  207. output.scrollTop = output.scrollHeight;
  208. }
  209. function ws_connect() {
  210. // check for websocket support
  211. // for Internet Explorer < 10 there are options for websocket support that
  212. // could be integrated into production code, but for now, we are expecting
  213. // browser support to be present for this demo
  214. if ('WebSocket' in window) {
  215. writeToScreen('Connecting');
  216. connection = new WebSocket('ws://' + window.location.host + '/meters');
  217. connection.onopen = function(ev) {
  218. document.getElementById("connection").innerHTML = "WebSocket Disconnect";
  219. document.getElementById("update").disabled=false;
  220. document.getElementById("update").innerHTML = "Disable Update";
  221. writeToScreen('CONNECTED');
  222. var message = 'update on';
  223. writeToScreen('SENT: ' + message);
  224. connection.send(message);
  225. };
  226. connection.onclose = function(ev) {
  227. document.getElementById("update").disabled=true;
  228. document.getElementById("update").innerHTML = "Enable Update";
  229. document.getElementById("connection").innerHTML = "WebSocket Connect";
  230. writeToScreen('DISCONNECTED');
  231. };
  232. connection.onmessage = function(ev) {
  233. if (ev.data.substr(0,5) == "meter")
  234. {
  235. var target = ev.data.split(":")[0];
  236. var meter = document.getElementById(target);
  237. var data = ev.data.split(":")[1].split(",");
  238. var percent = (data[0]*100)/data[1];
  239. meter.style.width = percent+"%";
  240. }
  241. else
  242. writeToScreen('RECEIVED: ' + ev.data);
  243. };
  244. connection.onerror = function(ev) {
  245. alert("WebSocket error");
  246. };
  247. } else {
  248. alert("WebSocket is not available!!!\n" +
  249. "Demo will not function.");
  250. }
  251. }
  252. // user connect/disconnect
  253. function toggleConnection(el) {
  254. var tag=el.innerHTML;
  255. if (tag == "WebSocket Connect")
  256. {
  257. ws_connect();
  258. }
  259. else
  260. {
  261. connection.close();
  262. }
  263. }
  264. // user turn updates on/off
  265. function toggleUpdate(el) {
  266. var tag=el.innerHTML;
  267. var message;
  268. if (tag == "Enable Update")
  269. {
  270. message = 'update on';
  271. el.innerHTML = "Disable Update";
  272. }
  273. else
  274. {
  275. message = 'update off';
  276. el.innerHTML = "Enable Update";
  277. }
  278. writeToScreen('SENT: ' + message);
  279. connection.send(message);
  280. }
  281. </script>
  282. </html>