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