1
0
Fork 0
mirror of https://github.com/warmcat/libwebsockets.git synced 2025-03-16 00:00:07 +01:00
libwebsockets/minimal-examples/ws-server/minimal-ws-broker/mount-origin/example.js
Andy Green d9f982a055 minimal examples: update for CSP best practices
1) update the logos to svg

2) add svg icon for strict security policy where used

3) define new vhost option flag to enforce sending CSP headers
   with the result code

4) add vhost option flag to minimal examples to
   enforce sending CSP where applicable

5) Go through all the affecting examples confirming they
   still work

6) add LWS_RECOMMENDED_MIN_HEADER_SPACE constant (currently
   2048) to clarify when we need a buffer to hold headers...
   with CSP the headers have become potentially a lot
   larger.
2018-11-21 17:03:29 +08:00

83 lines
1.8 KiB
JavaScript

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;
}
function new_ws(urlpath, protocol)
{
if (typeof MozWebSocket != "undefined")
return new MozWebSocket(urlpath, protocol);
return new WebSocket(urlpath, protocol);
}
document.addEventListener("DOMContentLoaded", function() {
subscriber_ws = new_ws(get_appropriate_ws_url(""), "lws-minimal-broker");
try {
subscriber_ws.onopen = function() {
document.getElementById("b").disabled = 0;
};
subscriber_ws.onmessage =function got_packet(msg) {
document.getElementById("r").value =
document.getElementById("r").value + msg.data + "\n";
document.getElementById("r").scrollTop =
document.getElementById("r").scrollHeight;
};
subscriber_ws.onclose = function(){
document.getElementById("b").disabled = 1;
};
} catch(exception) {
alert("<p>Error " + exception);
}
publisher_ws = new_ws(get_appropriate_ws_url("/publisher"), "lws-minimal-broker");
try {
publisher_ws.onopen = function() {
document.getElementById("m").disabled = 0;
};
publisher_ws.onmessage =function got_packet(msg) {
};
publisher_ws.onclose = function(){
document.getElementById("m").disabled = 1;
};
} catch(exception) {
alert("<p>Error " + exception);
}
function sendmsg()
{
publisher_ws.send(document.getElementById("m").value);
document.getElementById("m").value = "";
}
document.getElementById("b").addEventListener("click", sendmsg);
}, false);