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-pmd-bulk"); 36 try { 37 ws.onopen = function() { 38 document.getElementById("r").disabled = 0; 39 document.getElementById("status").textContent = "ws open "+ ws.extensions; 40 }; 41 42 ws.onmessage = function got_packet(msg) { 43 console.log("Received ws message len " + msg.data.size); 44 document.getElementById("r").value = 45 document.getElementById("r").value + "\nReceived: " + msg.data.size + " bytes\n"; 46 document.getElementById("r").scrollTop = 47 document.getElementById("r").scrollHeight; 48 49 /* echo it back */ 50 ws.send(msg.data); 51 }; 52 53 ws.onclose = function(){ 54 document.getElementById("r").disabled = 1; 55 document.getElementById("status").textContent = "ws closed"; 56 }; 57 } catch(exception) { 58 alert("<p>Error " + exception); 59 } 60 61}, false); 62 63