This commit is contained in:
2026-01-11 09:52:46 +03:00
parent 4b5db1d472
commit 629ab310b6
8 changed files with 805 additions and 9 deletions

View File

@@ -0,0 +1,40 @@
var gateway = `ws://${window.location.hostname}/ws`;
var websocket;
window.addEventListener('load', onLoad);
function initWebSocket() {
console.log('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');
}
function onClose(event) {
console.log('Connection closed');
setTimeout(initWebSocket, 2000);
}
function onMessage(event) {
var state;
console.log(event.data);
if (event.data == "1") {
state = "ON";
}
else {
state = "OFF";
}
document.getElementById('state').innerHTML = state;
}
function onLoad(event) {
initWebSocket();
initButton();
}
function initButton() {
document.getElementById('button').addEventListener('click', toggle);
}
function toggle() {
console.log('Click');
websocket.send('toggle');
}