From 80cd776337034bae7f044f2b2b865a86699db8e0 Mon Sep 17 00:00:00 2001 From: "kevin.larke" Date: Tue, 31 Mar 2020 12:56:00 -0400 Subject: [PATCH] html/websockSrvTest.hmtl : Updated to get URL from DOM and send to particular or all connected endpoints. --- html/websockSrvTest/test_websocket.html | 58 ++++++++++++++++++++++--- 1 file changed, 51 insertions(+), 7 deletions(-) diff --git a/html/websockSrvTest/test_websocket.html b/html/websockSrvTest/test_websocket.html index 87467fb..6be125a 100644 --- a/html/websockSrvTest/test_websocket.html +++ b/html/websockSrvTest/test_websocket.html @@ -10,9 +10,13 @@
Outgoing text message to the server. - - + + +
+ Send - loopback to this client +
+ Broadcast - transmit to all clients
@@ -28,7 +32,35 @@ return new WebSocket(urlpath, protocol); } - subscriber_ws = new_ws("ws://localhost:7681/", "websocksrv_test_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() { @@ -38,7 +70,9 @@ subscriber_ws.onmessage = function got_packet(msg) { - document.getElementById("r").value = document.getElementById("r").value + msg.data + "\n"; + 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; } @@ -53,7 +87,9 @@ alert('

Error' + exception); } - publisher_ws = new_ws("ws://localhost:7681//publisher", "websocksrv_test_protocol"); + url = get_appropriate_ws_url("publisher"); + + publisher_ws = new_ws(url, "websocksrv_test_protocol"); try { publisher_ws.onopen = function() @@ -64,7 +100,11 @@ publisher_ws.onmessage = function got_packet(msg) { - console.log("rcv:"+msg.data) + 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() @@ -78,9 +118,13 @@ alert('

Error' + exception); } - function sendmsg() + function sendmsg( arg ) { var val = document.getElementById("msgTextId").value + + if( arg === "bcast" ) + val = "bcast "+val; + publisher_ws.send(val); document.getElementById("msgTextId").value = ""; }