141 lines
3.3 KiB
HTML
141 lines
3.3 KiB
HTML
<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>
|
|
<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
|
|
<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);
|
|
}
|
|
|
|
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");
|
|
try {
|
|
subscriber_ws.onopen = function()
|
|
{
|
|
document.getElementById("quitBtnId").disabled = 0;
|
|
document.getElementById("sendBtnId").disabled = 0;
|
|
}
|
|
|
|
subscriber_ws.onmessage = function got_packet(msg)
|
|
{
|
|
console.log("rcv1:"+msg.data)
|
|
|
|
document.getElementById("r").value = document.getElementById("r").value + " 1:" + msg.data + "\n";
|
|
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);
|
|
}
|
|
|
|
url = get_appropriate_ws_url("publisher");
|
|
|
|
publisher_ws = new_ws(url, "websocksrv_test_protocol");
|
|
try {
|
|
|
|
publisher_ws.onopen = function()
|
|
{
|
|
document.getElementById("msgTextId").disabled = 0;
|
|
document.getElementById("titleDivId").style.color = '#00ff00'
|
|
}
|
|
|
|
publisher_ws.onmessage = function got_packet(msg)
|
|
{
|
|
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;
|
|
|
|
}
|
|
|
|
publisher_ws.onclose = function()
|
|
{
|
|
document.getElementById("msgTextId").disabled = 1;
|
|
document.getElementById("titleDivId").style.color = '#ff0000'
|
|
}
|
|
|
|
} catch(exception)
|
|
{
|
|
alert('<p>Error' + exception);
|
|
}
|
|
|
|
function sendmsg( arg )
|
|
{
|
|
var val = document.getElementById("msgTextId").value
|
|
|
|
if( arg === "bcast" )
|
|
val = "bcast "+val;
|
|
|
|
publisher_ws.send(val);
|
|
document.getElementById("msgTextId").value = "";
|
|
}
|
|
|
|
function sendquit()
|
|
{
|
|
publisher_ws.send("quit");
|
|
}
|
|
|
|
</script>
|
|
|
|
</html>
|
|
|