Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 1 | <!doctype html> |
| 2 | <html> |
| 3 | <head> |
| 4 | <title>WebSocket++ Telemetry Client</title> |
| 5 | </head> |
| 6 | <body> |
| 7 | |
| 8 | <script type="text/javascript"> |
| 9 | var ws; |
| 10 | var url; |
| 11 | |
| 12 | function connect() { |
| 13 | url = document.getElementById("server_url").value; |
| 14 | |
| 15 | if ("WebSocket" in window) { |
| 16 | ws = new WebSocket(url); |
| 17 | } else if ("MozWebSocket" in window) { |
| 18 | ws = new MozWebSocket(url); |
| 19 | } else { |
| 20 | document.getElementById("messages").innerHTML += "This Browser does not support WebSockets<br />"; |
| 21 | return; |
| 22 | } |
| 23 | ws.onopen = function(e) { |
| 24 | document.getElementById("messages").innerHTML += "Client: A connection to "+ws.url+" has been opened.<br />"; |
| 25 | |
| 26 | document.getElementById("server_url").disabled = true; |
| 27 | document.getElementById("toggle_connect").innerHTML = "Disconnect"; |
| 28 | }; |
| 29 | |
| 30 | ws.onerror = function(e) { |
| 31 | document.getElementById("messages").innerHTML += "Client: An error occured, see console log for more details.<br />"; |
| 32 | console.log(e); |
| 33 | }; |
| 34 | |
| 35 | ws.onclose = function(e) { |
| 36 | document.getElementById("messages").innerHTML += "Client: The connection to "+url+" was closed. ["+e.code+(e.reason != "" ? ","+e.reason : "")+"]<br />"; |
| 37 | cleanup_disconnect(); |
| 38 | }; |
| 39 | |
| 40 | ws.onmessage = function(e) { |
| 41 | document.getElementById("messages").innerHTML += "Server: "+e.data+"<br />"; |
| 42 | }; |
| 43 | } |
| 44 | |
| 45 | function disconnect() { |
| 46 | ws.close(); |
| 47 | cleanup_disconnect(); |
| 48 | } |
| 49 | |
| 50 | function cleanup_disconnect() { |
| 51 | document.getElementById("server_url").disabled = false; |
| 52 | document.getElementById("toggle_connect").innerHTML = "Connect"; |
| 53 | } |
| 54 | |
| 55 | function toggle_connect() { |
| 56 | if (document.getElementById("server_url").disabled === false) { |
| 57 | connect(); |
| 58 | } else { |
| 59 | disconnect(); |
| 60 | } |
| 61 | } |
| 62 | </script> |
| 63 | |
| 64 | <style> |
| 65 | body,html { |
| 66 | margin: 0px; |
| 67 | padding: 0px; |
| 68 | } |
| 69 | #controls { |
| 70 | float:right; |
| 71 | background-color: #999; |
| 72 | } |
| 73 | |
| 74 | </style> |
| 75 | |
| 76 | <div id="controls"> |
| 77 | <div id="server"> |
Ed Tanous | c4771fb | 2017-03-13 13:39:49 -0700 | [diff] [blame^] | 78 | <input type="text" name="server_url" id="server_url" value="wss://localhost?session_id=foobar" /><br /> |
Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 79 | <button id="toggle_connect" onclick="toggle_connect();">Connect</button> |
| 80 | </div> |
| 81 | </div> |
| 82 | <div id="messages"></div> |
| 83 | |
| 84 | </body> |
| 85 | </html> |