| 1 | <!DOCTYPE html> |
| 2 | <html lang="en"> |
| 3 | <head> |
| 4 | <meta charset="utf-8"/> |
| 5 | <title>Websocket client</title> |
| 6 | </head> |
| 7 | |
| 8 | <body> |
| 9 | |
| 10 | <header> |
| 11 | <h1>Websocket client</h1> |
| 12 | <div id="status"></div> |
| 13 | </header> |
| 14 | |
| 15 | <nav> |
| 16 | <div id="connecting"> |
| 17 | <input type='text' id="server" value=""></input> |
| 18 | <button type="button" onclick="toggle_connection()">connection</button> |
| 19 | </div> |
| 20 | |
| 21 | <div id="connected"> |
| 22 | <input type='text' id="message" value=""></input> |
| 23 | <button type="button" onclick="sendTxt();">send</button> |
| 24 | </div> |
| 25 | </nav> |
| 26 | |
| 27 | <main id="content"> |
| 28 | <button id="clear" onclick="clearScreen()" >Clear text</button> |
| 29 | <div id="output"></div> |
| 30 | </main> |
| 31 | |
| 32 | <script type="text/javascript"> |
| 33 | |
| 34 | var websocket; |
| 35 | var server = document.getElementById("server"); |
| 36 | var message = document.getElementById("message"); |
| 37 | var connecting = document.getElementById("connecting"); |
| 38 | var connected = document.getElementById("connected"); |
| 39 | var content = document.getElementById("content"); |
| 40 | var output = document.getElementById("output"); |
| 41 | |
| 42 | server.value = "ws://" + window.location.host + "/websocket"; |
| 43 | connected.style.display = "none"; |
| 44 | content.style.display = "none"; |
| 45 | |
| 46 | function connect() |
| 47 | { |
| 48 | wsHost = server.value; |
| 49 | websocket = new WebSocket(wsHost); |
| 50 | showScreen('<b>Connecting to: ' + wsHost + '</b>'); |
| 51 | websocket.onopen = function(evt) { onOpen(evt) }; |
| 52 | websocket.onclose = function(evt) { onClose(evt) }; |
| 53 | websocket.onmessage = function(evt) { onMessage(evt) }; |
| 54 | websocket.onerror = function(evt) { onError(evt) }; |
| 55 | }; |
| 56 | |
| 57 | function disconnect() { |
| 58 | websocket.close(); |
| 59 | }; |
| 60 | |
| 61 | function toggle_connection(){ |
| 62 | if (websocket && websocket.readyState == websocket.OPEN) { |
| 63 | disconnect(); |
| 64 | } else { |
| 65 | connect(); |
| 66 | }; |
| 67 | }; |
| 68 | |
| 69 | function sendTxt() { |
| 70 | if (websocket.readyState == websocket.OPEN) { |
| 71 | var msg = message.value; |
| 72 | websocket.send(msg); |
| 73 | showScreen('sending: ' + msg); |
| 74 | } else { |
| 75 | showScreen('websocket is not connected'); |
| 76 | }; |
| 77 | }; |
| 78 | |
| 79 | function onOpen(evt) { |
| 80 | showScreen('<span style="color: green;">CONNECTED </span>'); |
| 81 | connecting.style.display = "none"; |
| 82 | connected.style.display = ""; |
| 83 | content.style.display = ""; |
| 84 | }; |
| 85 | |
| 86 | function onClose(evt) { |
| 87 | showScreen('<span style="color: red;">DISCONNECTED</span>'); |
| 88 | }; |
| 89 | |
| 90 | function onMessage(evt) { |
| 91 | showScreen('<span style="color: blue;">RESPONSE: ' + evt.data + '</span>'); |
| 92 | }; |
| 93 | |
| 94 | function onError(evt) { |
| 95 | showScreen('<span style="color: red;">ERROR: ' + evt.data + '</span>'); |
| 96 | }; |
| 97 | |
| 98 | function showScreen(html) { |
| 99 | var el = document.createElement("p"); |
| 100 | el.innerHTML = html; |
| 101 | output.insertBefore(el, output.firstChild); |
| 102 | }; |
| 103 | |
| 104 | function clearScreen() { |
| 105 | output.innerHTML = ""; |
| 106 | }; |
| 107 | |
| 108 | </script> |
| 109 | </body> |
| 110 | </html> |