1 2function get_appropriate_ws_url(extra_url) 3{ 4 var pcol; 5 var u = document.URL; 6 7 /* 8 * We open the websocket encrypted if this page came on an 9 * https:// url itself, otherwise unencrypted 10 */ 11 12 if (u.substring(0, 5) === "https") { 13 pcol = "wss://"; 14 u = u.substr(8); 15 } else { 16 pcol = "ws://"; 17 if (u.substring(0, 4) === "http") 18 u = u.substr(7); 19 } 20 21 u = u.split("/"); 22 23 /* + "/xxx" bit is for IE10 workaround */ 24 25 return pcol + u[0] + "/" + extra_url; 26} 27 28function new_ws(urlpath, protocol) 29{ 30 return new WebSocket(urlpath, protocol); 31} 32 33document.addEventListener("DOMContentLoaded", function() { 34 35 var ws = new_ws(get_appropriate_ws_url(""), "lws-minimal"); 36 try { 37 ws.onopen = function() { 38 document.getElementById("m").disabled = 0; 39 document.getElementById("b").disabled = 0; 40 document.getElementById("status").textContent = "ws open "+ ws.extensions; 41 }; 42 43 ws.onmessage =function got_packet(msg) { 44 document.getElementById("r").value = 45 document.getElementById("r").value + msg.data + "\n"; 46 document.getElementById("r").scrollTop = 47 document.getElementById("r").scrollHeight; 48 }; 49 50 ws.onclose = function(){ 51 document.getElementById("m").disabled = 1; 52 document.getElementById("b").disabled = 1; 53 document.getElementById("status").textContent = "ws closed"; 54 }; 55 } catch(exception) { 56 alert("<p>Error " + exception); 57 } 58 59 function sendmsg() 60 { 61 ws.send(document.getElementById("m").value); 62 document.getElementById("m").value = ""; 63 } 64 65 document.getElementById("b").addEventListener("click", sendmsg); 66 67}, false); 68 69