backend/socket_test.html

41 lines
1.1 KiB
HTML
Raw Normal View History

2020-03-15 12:41:57 +00:00
<!-- vim: set ts=2 sw=2 et tw=80: -->
2020-03-15 09:44:10 +00:00
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<div id="malusa">
<h1>Waiting for authentication...</h1>
</div>
<script>
let malusa = document.getElementById("malusa");
let connection = new WebSocket("ws://localhost:8080/sensor-socket");
console.log("***CREATED WEBSOCKET");
connection.onopen = function(evt) {
console.log("***ONOPEN", evt);
connection.send(JSON.stringify({token: prompt("insert authentication token")}));
};
connection.onmessage = function(evt) {
console.log("***ONMESSAGE", evt);
let data = JSON.parse(evt.data);
if (data.authenticated) {
malusa.innerHTML = "<h1>Socket is now authenticated!</h1>" +
"<img src='https://maggioni.xyz/astley.gif'>";
} else if (data.authenticated === false) {
malusa.innerHTML = "<h1>Authentication error</h1>";
2020-03-15 12:41:57 +00:00
} else {
malusa.innerHTML += "<p><pre>" + JSON.stringify(JSON.parse(evt.data), null, 2) + "</pre></p>";
2020-03-15 09:44:10 +00:00
}
};
connection.onerror = function(evt) {
console.error("***ONERROR", evt);
};
</script>
</body>
</html>