| 1 |
|
| 2 |
function 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 |
|
| 28 |
function new_ws(urlpath, protocol) |
| 29 |
{ |
| 30 |
return new WebSocket(urlpath, protocol); |
| 31 |
} |
| 32 |
|
| 33 |
document.addEventListener("DOMContentLoaded", function() { |
| 34 |
|
| 35 |
var subscriber_ws = new_ws(get_appropriate_ws_url(""), "lws-minimal-broker"); |
| 36 |
try { |
| 37 |
subscriber_ws.onopen = function() { |
| 38 |
document.getElementById("b").disabled = 0; |
| 39 |
}; |
| 40 |
|
| 41 |
subscriber_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 |
subscriber_ws.onclose = function(){ |
| 49 |
document.getElementById("b").disabled = 1; |
| 50 |
}; |
| 51 |
} catch(exception) { |
| 52 |
alert("<p>Error " + exception); |
| 53 |
} |
| 54 |
|
| 55 |
var publisher_ws = new_ws(get_appropriate_ws_url("/publisher"), "lws-minimal-broker"); |
| 56 |
try { |
| 57 |
publisher_ws.onopen = function() { |
| 58 |
document.getElementById("m").disabled = 0; |
| 59 |
}; |
| 60 |
|
| 61 |
publisher_ws.onmessage =function got_packet(msg) { |
| 62 |
}; |
| 63 |
|
| 64 |
publisher_ws.onclose = function(){ |
| 65 |
document.getElementById("m").disabled = 1; |
| 66 |
}; |
| 67 |
} catch(exception) { |
| 68 |
alert("<p>Error " + exception); |
| 69 |
} |
| 70 |
|
| 71 |
function sendmsg() |
| 72 |
{ |
| 73 |
publisher_ws.send(document.getElementById("m").value); |
| 74 |
document.getElementById("m").value = ""; |
| 75 |
} |
| 76 |
|
| 77 |
document.getElementById("b").addEventListener("click", sendmsg); |
| 78 |
|
| 79 |
}, false); |
| 80 |
|