This is a http(s) server that supports also websocket. It sends back any data sent to it over a websocket. Signed-off-by: Jukka Rissanen <jukka.rissanen@linux.intel.com>
46 lines
912 B
JavaScript
46 lines
912 B
JavaScript
/*
|
|
* Copyright (c) 2017 Intel Corporation
|
|
*
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
*/
|
|
|
|
var connected;
|
|
var first_run;
|
|
var ws;
|
|
|
|
function init() {
|
|
ws = new WebSocket(location.origin.replace("http", "ws") + "/ws");
|
|
|
|
first_run = "true";
|
|
connected = "false";
|
|
|
|
ws.onopen = function() {
|
|
output("Websocket connection opened");
|
|
connected = "true";
|
|
};
|
|
|
|
ws.onmessage = function(e) {
|
|
output("Websocket data received: " + e.data);
|
|
};
|
|
|
|
ws.onclose = function() {
|
|
output("Websocket connection closed");
|
|
connected = "false";
|
|
};
|
|
|
|
ws.onerror = function(e) {
|
|
output("Websocket data error (see console)");
|
|
console.log(e)
|
|
};
|
|
}
|
|
|
|
function escape(str) {
|
|
return str.replace(/&/, "&").replace(/</, "<").
|
|
replace(/>/, ">").replace(/"/, """); // "
|
|
}
|
|
|
|
function output(str) {
|
|
var log = document.getElementById("output");
|
|
log.innerHTML += escape(str) + "<br>";
|
|
}
|