Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html>
- <head>
- <title>Button LED webpage</title>
- </head>
- <script language="javascript">
- function init() {
- var scheme
- if (window.location.protocol == 'https:')
- scheme = 'wss:';
- else
- scheme = 'ws:';
- var wsUri = scheme + '//' + window.location.hostname + '/ws';
- logMsg("Connecting to " + wsUri + "...")
- websocket = new WebSocket(wsUri);
- websocket.onopen = function(evt) { onOpen (evt) };
- websocket.onclose = function(evt) { onClose (evt) };
- websocket.onmessage = function(evt) { onMessage (evt) };
- websocket.onerror = function(evt) { onError (evt) };
- }
- function onOpen(evt) {
- logMsg("Connected");
- }
- function onClose(evt) {
- logMsg("Disconnected");
- }
- function onMessage(evt) {
- logMsg(evt.data);
- document.getElementById("button").innerHTML = evt.data;
- }
- function onError(evt) {
- logMsg('ERROR: ' + evt.data);
- }
- function logMsg(s) {
- document.getElementById("log").value += s + '\n';
- }
- function ledClick() {
- if (document.getElementById("led").checked)
- websocket.send('LED ON');
- else
- websocket.send('LED OFF');
- }
- window.addEventListener("load", init, false);
- </script>
- <body>
- <h1>Button LED webpage</h1>
- <input type="checkbox" id="led" onclick="ledClick()">LED<br>
- <p id="button">Button Off</p>
- <textarea id="log" rows="10" cols="100"></textarea>
- </body>
- </html>
Add Comment
Please, Sign In to add comment