index.html 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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. var count=1;
  203. function writeToScreen (message) {
  204. var div = document.createElement('div');
  205. var output = document.getElementById('output');
  206. div.innerHTML = message;
  207. output.appendChild(div);
  208. output.scrollTop = output.scrollHeight;
  209. }
  210. function send_counter() {
  211. if (connection.bufferedAmount == 0)
  212. {
  213. connection.send('counter ' + count);
  214. count = count+1;
  215. }
  216. }
  217. function ws_connect() {
  218. // check for websocket support
  219. // for Internet Explorer < 10 there are options for websocket support that
  220. // could be integrated into production code, but for now, we are expecting
  221. // browser support to be present for this demo
  222. if ('WebSocket' in window) {
  223. writeToScreen('Connecting');
  224. connection = new WebSocket('ws://' + window.location.host + '/meters');
  225. connection.onopen = function(ev) {
  226. document.getElementById("connection").innerHTML = "WebSocket Disconnect";
  227. document.getElementById("update").disabled=false;
  228. document.getElementById("update").innerHTML = "Disable Update";
  229. writeToScreen('CONNECTED');
  230. var message = 'update on';
  231. writeToScreen('SENT: ' + message);
  232. connection.send(message);
  233. };
  234. connection.onclose = function(ev) {
  235. clearInterval(counterid);
  236. document.getElementById("update").disabled=true;
  237. document.getElementById("update").innerHTML = "Enable Update";
  238. document.getElementById("connection").innerHTML = "WebSocket Connect";
  239. writeToScreen('DISCONNECTED');
  240. };
  241. connection.onmessage = function(ev) {
  242. if (ev.data.substr(0,5) == "meter")
  243. {
  244. var target = ev.data.split(":")[0];
  245. var meter = document.getElementById(target);
  246. var data = ev.data.split(":")[1].split(",");
  247. var percent = (data[0]*100)/data[1];
  248. meter.style.width = percent+"%";
  249. }
  250. else
  251. {
  252. writeToScreen('RECEIVED: ' + ev.data);
  253. }
  254. send_counter();
  255. };
  256. connection.onerror = function(ev) {
  257. alert("WebSocket error");
  258. };
  259. counterid = setInterval(send_counter, 10);
  260. } else {
  261. alert("WebSocket is not available!!!\n" +
  262. "Demo will not function.");
  263. }
  264. }
  265. // user connect/disconnect
  266. function toggleConnection(el) {
  267. var tag=el.innerHTML;
  268. if (tag == "WebSocket Connect")
  269. {
  270. ws_connect();
  271. }
  272. else
  273. {
  274. connection.close();
  275. }
  276. }
  277. // user turn updates on/off
  278. function toggleUpdate(el) {
  279. var tag=el.innerHTML;
  280. var message;
  281. if (tag == "Enable Update")
  282. {
  283. message = 'update on';
  284. el.innerHTML = "Disable Update";
  285. }
  286. else
  287. {
  288. message = 'update off';
  289. el.innerHTML = "Enable Update";
  290. }
  291. writeToScreen('SENT: ' + message);
  292. connection.send(message);
  293. }
  294. </script>
  295. </html>