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(""), "timer"); 36 try { 37 ws.onopen = function() { 38 document.getElementById("m").disabled = 0; 39 document.getElementById("b").disabled = 0; 40 }; 41 42 ws.onmessage =function got_packet(msg) { 43 document.getElementById("r").value = 44 document.getElementById("r").value + msg.data + "\n"; 45 document.getElementById("r").scrollTop = 46 document.getElementById("r").scrollHeight; 47 }; 48 49 ws.onclose = function(){ 50 document.getElementById("m").disabled = 1; 51 document.getElementById("b").disabled = 1; 52 }; 53 } catch(exception) { 54 alert("<p>Error " + exception); 55 } 56 57 function sendmsg() 58 { 59 ws.send(document.getElementById("m").value); 60 document.getElementById("m").value = ""; 61 } 62 63 document.getElementById("b").addEventListener("click", sendmsg); 64 65}, false); 66 67