Files
AI-on-the-edge-device/sd-card/html/websocket.html
2023-04-30 21:37:35 +02:00

66 lines
1.4 KiB
HTML

<html>
<head>
<style>
#rx {
font-family: 'Courier New', Courier, monospace;
font-size: small;
}
</style>
<script type="text/javascript" src="common.js?v=$COMMIT_HASH"></script>
</head>
<body>
<div id="rx"></div>
<script>
var gateway = getDomainname().replace("http://", "ws://") + "/ws";
var websocket;
window.addEventListener('load', onLoad);
function initWebSocket() {
console.log('Trying to open a WebSocket connection...');
addToLog('Trying to open a WebSocket connection...');
websocket = new WebSocket(gateway);
websocket.onopen = onOpen;
websocket.onclose = onClose;
websocket.onmessage = onMessage; // <-- add this line
}
function onOpen(event) {
console.log('Connection opened');
addToLog('Connection opened');
}
function onClose(event) {
console.log('Connection closed');
addToLog('Connection closed');
setTimeout(initWebSocket, 2000);
}
function onMessage(event) {
console.log(event);
addToLog(event.data);
}
function onLoad(event) {
initWebSocket();
}
function addToLog(msg) {
document.getElementById('rx').innerHTML += "[" + new Date().toLocaleTimeString() + "] " +msg + "<br>\n";
window.scrollBy(0,document.body.scrollHeight);
}
</script>
</body>
</html>