Guest User

index.html

a guest
May 3rd, 2023
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 1.79 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3.     <head>
  4.         <title>Button LED webpage</title>
  5.     </head>
  6.  
  7.     <script language="javascript">
  8.         function init() {
  9.             var scheme
  10.             if (window.location.protocol == 'https:')
  11.                 scheme = 'wss:';
  12.             else
  13.                 scheme = 'ws:';
  14.             var wsUri = scheme + '//' + window.location.hostname + '/ws';
  15.             logMsg("Connecting to " + wsUri + "...")
  16.             websocket           = new WebSocket(wsUri);
  17.             websocket.onopen    = function(evt) { onOpen    (evt) };
  18.             websocket.onclose   = function(evt) { onClose   (evt) };
  19.             websocket.onmessage = function(evt) { onMessage (evt) };
  20.             websocket.onerror   = function(evt) { onError   (evt) };
  21.         }
  22.    
  23.         function onOpen(evt) {
  24.             logMsg("Connected");
  25.         }
  26.  
  27.         function onClose(evt) {
  28.             logMsg("Disconnected");
  29.         }
  30.  
  31.         function onMessage(evt) {
  32.             logMsg(evt.data);
  33.             document.getElementById("button").innerHTML = evt.data;
  34.         }
  35.  
  36.         function onError(evt) {
  37.             logMsg('ERROR: ' + evt.data);
  38.         }
  39.  
  40.         function logMsg(s) {
  41.             document.getElementById("log").value += s + '\n';
  42.         }
  43.  
  44.         function ledClick() {
  45.             if (document.getElementById("led").checked)
  46.                 websocket.send('LED ON');
  47.             else
  48.                 websocket.send('LED OFF');
  49.         }
  50.  
  51.         window.addEventListener("load", init, false);
  52.     </script>
  53.  
  54.     <body>
  55.         <h1>Button LED webpage</h1>
  56.         <input type="checkbox" id="led"  onclick="ledClick()">LED<br>
  57.         <p id="button">Button Off</p>
  58.         <textarea id="log" rows="10" cols="100"></textarea>
  59.     </body>
  60. </html>
Add Comment
Please, Sign In to add comment