index.htm 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <!--
  2. FSWebServer - Example Index Page
  3. Copyright (c) 2015 Hristo Gochkov. All rights reserved.
  4. This file is part of the ESP8266WebServer library for Arduino environment.
  5. This library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  9. This library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with this library; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  16. -->
  17. <!DOCTYPE html>
  18. <html>
  19. <head>
  20. <meta http-equiv="Content-type" content="text/html; charset=utf-8">
  21. <title>WebSocketTester</title>
  22. <style type="text/css" media="screen">
  23. body {
  24. margin:0;
  25. padding:0;
  26. background-color: black;
  27. }
  28. #dbg, #input_div, #input_el {
  29. font-family: monaco;
  30. font-size: 12px;
  31. line-height: 13px;
  32. color: #AAA;
  33. }
  34. #dbg, #input_div {
  35. margin:0;
  36. padding:0;
  37. padding-left:4px;
  38. }
  39. #input_el {
  40. width:98%;
  41. background-color: rgba(0,0,0,0);
  42. border: 0px;
  43. }
  44. #input_el:focus {
  45. outline: none;
  46. }
  47. </style>
  48. <script type="text/javascript">
  49. var ws = null;
  50. function ge(s){ return document.getElementById(s);}
  51. function ce(s){ return document.createElement(s);}
  52. function stb(){ window.scrollTo(0, document.body.scrollHeight || document.documentElement.scrollHeight); }
  53. function sendBlob(str){
  54. var buf = new Uint8Array(str.length);
  55. for (var i = 0; i < str.length; ++i) buf[i] = str.charCodeAt(i);
  56. ws.send(buf);
  57. }
  58. function addMessage(m){
  59. var msg = ce("div");
  60. msg.innerText = m;
  61. ge("dbg").appendChild(msg);
  62. stb();
  63. }
  64. function startSocket(){
  65. ws = new WebSocket('ws://'+document.location.host+'/ws',['arduino']);
  66. ws.binaryType = "arraybuffer";
  67. ws.onopen = function(e){
  68. addMessage("Connected");
  69. };
  70. ws.onclose = function(e){
  71. addMessage("Disconnected");
  72. };
  73. ws.onerror = function(e){
  74. console.log("ws error", e);
  75. addMessage("Error");
  76. };
  77. ws.onmessage = function(e){
  78. var msg = "";
  79. if(e.data instanceof ArrayBuffer){
  80. msg = "BIN:";
  81. var bytes = new Uint8Array(e.data);
  82. for (var i = 0; i < bytes.length; i++) {
  83. msg += String.fromCharCode(bytes[i]);
  84. }
  85. } else {
  86. msg = "TXT:"+e.data;
  87. }
  88. addMessage(msg);
  89. };
  90. ge("input_el").onkeydown = function(e){
  91. stb();
  92. if(e.keyCode == 13 && ge("input_el").value != ""){
  93. ws.send(ge("input_el").value);
  94. ge("input_el").value = "";
  95. }
  96. }
  97. }
  98. function startEvents(){
  99. var es = new EventSource('/events');
  100. es.onopen = function(e) {
  101. addMessage("Events Opened");
  102. };
  103. es.onerror = function(e) {
  104. if (e.target.readyState != EventSource.OPEN) {
  105. addMessage("Events Closed");
  106. }
  107. };
  108. es.onmessage = function(e) {
  109. addMessage("Event: " + e.data);
  110. };
  111. es.addEventListener('ota', function(e) {
  112. addMessage("Event[ota]: " + e.data);
  113. }, false);
  114. }
  115. function onBodyLoad(){
  116. startSocket();
  117. startEvents();
  118. }
  119. </script>
  120. </head>
  121. <body id="body" onload="onBodyLoad()">
  122. <pre id="dbg"></pre>
  123. <div id="input_div">
  124. $<input type="text" value="" id="input_el">
  125. </div>
  126. </body>
  127. </html>