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