2019-12-24 15:05:24 +00:00
|
|
|
<meta charset="UTF-8">
|
|
|
|
<html>
|
|
|
|
<body>
|
|
|
|
|
|
|
|
<div id="titleDivId">Websock Test App</div><br>
|
|
|
|
Incoming text messages from the server.
|
|
|
|
<br>
|
|
|
|
<br>
|
|
|
|
<textarea id=r readonly cols=40 rows=10></textarea><br>
|
|
|
|
<br>
|
|
|
|
|
|
|
|
Outgoing text message to the server.
|
|
|
|
<input type="text" id="msgTextId" cols=40 rows=1>
|
2020-03-31 16:56:00 +00:00
|
|
|
<button id="sendBtnId", onclick="sendmsg('');">Send</button>
|
|
|
|
<button id="sendAllBtnId", onclick="sendmsg('bcast');">Broadcast</button>
|
|
|
|
<br>
|
|
|
|
Send - loopback to this client
|
|
|
|
<br>
|
|
|
|
Broadcast - transmit to all clients
|
2019-12-24 15:05:24 +00:00
|
|
|
<br>
|
|
|
|
<button id="quitBtnId", onclick="sendquit();">Quit</button>
|
|
|
|
</body>
|
|
|
|
|
|
|
|
|
|
|
|
<script>
|
|
|
|
|
|
|
|
function new_ws(urlpath, protocol)
|
|
|
|
{
|
|
|
|
if (typeof MozWebSocket != "undefined")
|
|
|
|
return new MozWebSocket(urlpath, protocol);
|
|
|
|
|
|
|
|
return new WebSocket(urlpath, protocol);
|
|
|
|
}
|
|
|
|
|
2020-03-31 16:56:00 +00:00
|
|
|
function get_appropriate_ws_url(extra_url)
|
|
|
|
{
|
|
|
|
var pcol;
|
|
|
|
var u = document.URL;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We open the websocket encrypted if this page came on an
|
|
|
|
* https:// url itself, otherwise unencrypted
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (u.substring(0, 5) === "https") {
|
|
|
|
pcol = "wss://";
|
|
|
|
u = u.substr(8);
|
|
|
|
} else {
|
|
|
|
pcol = "ws://";
|
|
|
|
if (u.substring(0, 4) === "http")
|
|
|
|
u = u.substr(7);
|
|
|
|
}
|
|
|
|
|
|
|
|
u = u.split("/");
|
|
|
|
|
|
|
|
/* + "/xxx" bit is for IE10 workaround */
|
|
|
|
|
|
|
|
return pcol + u[0] + "/" + extra_url;
|
|
|
|
}
|
|
|
|
|
|
|
|
url = get_appropriate_ws_url("");
|
|
|
|
|
|
|
|
subscriber_ws = new_ws(url, "websocksrv_test_protocol");
|
2019-12-24 15:05:24 +00:00
|
|
|
try {
|
|
|
|
subscriber_ws.onopen = function()
|
|
|
|
{
|
|
|
|
document.getElementById("quitBtnId").disabled = 0;
|
|
|
|
document.getElementById("sendBtnId").disabled = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
subscriber_ws.onmessage = function got_packet(msg)
|
|
|
|
{
|
2020-03-31 16:56:00 +00:00
|
|
|
console.log("rcv1:"+msg.data)
|
|
|
|
|
|
|
|
document.getElementById("r").value = document.getElementById("r").value + " 1:" + msg.data + "\n";
|
2019-12-24 15:05:24 +00:00
|
|
|
document.getElementById("r").scrollTop = document.getElementById("r").scrollHeight;
|
|
|
|
}
|
|
|
|
|
|
|
|
subscriber_ws.onclose = function()
|
|
|
|
{
|
|
|
|
document.getElementById("quitBtnId").disabled = 1;
|
|
|
|
document.getElementById("sendBtnId").disabled = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch(exception)
|
|
|
|
{
|
|
|
|
alert('<p>Error' + exception);
|
|
|
|
}
|
|
|
|
|
2020-03-31 16:56:00 +00:00
|
|
|
url = get_appropriate_ws_url("publisher");
|
|
|
|
|
|
|
|
publisher_ws = new_ws(url, "websocksrv_test_protocol");
|
2019-12-24 15:05:24 +00:00
|
|
|
try {
|
|
|
|
|
|
|
|
publisher_ws.onopen = function()
|
|
|
|
{
|
|
|
|
document.getElementById("msgTextId").disabled = 0;
|
|
|
|
document.getElementById("titleDivId").style.color = '#00ff00'
|
|
|
|
}
|
|
|
|
|
|
|
|
publisher_ws.onmessage = function got_packet(msg)
|
|
|
|
{
|
2020-03-31 16:56:00 +00:00
|
|
|
console.log("rcv2:"+msg.data)
|
|
|
|
|
|
|
|
document.getElementById("r").value = document.getElementById("r").value + " 2: "+msg.data + "\n";
|
|
|
|
document.getElementById("r").scrollTop = document.getElementById("r").scrollHeight;
|
|
|
|
|
2019-12-24 15:05:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
publisher_ws.onclose = function()
|
|
|
|
{
|
|
|
|
document.getElementById("msgTextId").disabled = 1;
|
|
|
|
document.getElementById("titleDivId").style.color = '#ff0000'
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch(exception)
|
|
|
|
{
|
|
|
|
alert('<p>Error' + exception);
|
|
|
|
}
|
|
|
|
|
2020-03-31 16:56:00 +00:00
|
|
|
function sendmsg( arg )
|
2019-12-24 15:05:24 +00:00
|
|
|
{
|
|
|
|
var val = document.getElementById("msgTextId").value
|
2020-03-31 16:56:00 +00:00
|
|
|
|
|
|
|
if( arg === "bcast" )
|
|
|
|
val = "bcast "+val;
|
|
|
|
|
2019-12-24 15:05:24 +00:00
|
|
|
publisher_ws.send(val);
|
|
|
|
document.getElementById("msgTextId").value = "";
|
|
|
|
}
|
|
|
|
|
|
|
|
function sendquit()
|
|
|
|
{
|
|
|
|
publisher_ws.send("quit");
|
|
|
|
}
|
|
|
|
|
|
|
|
</script>
|
|
|
|
|
|
|
|
</html>
|
|
|
|
|