iw4x-client/deps/mongoose/examples/webui-push-ws/web_root/index.html

40 lines
1.1 KiB
HTML
Raw Normal View History

2024-03-07 05:13:50 -05:00
<!DOCTYPE html>
<html lang="en">
<body>
<h1>Data push over WebSocket demo</h1>
<div style="margin-top: 1em;">Event log:</div>
<div id="log" style="background: #eee; height: 10em; padding: 0.5em; overflow:auto;"></div>
</body>
<script>
var log = document.getElementById('log');
const watch = function() {
var l = window.location, proto = l.protocol.replace('http', 'ws');
var tid, wsURI = proto + '//' + l.host + '/api/watch'
var reconnect = function() {
var ws = new WebSocket(wsURI);
ws.onopen = function() { log.innerHTML += 'CONNECTED<br/>'; }
ws.onmessage = function(ev) {
console.log(ev.data);
try {
var msg = JSON.parse(ev.data);
log.innerHTML += 'RECEIVED: ' + JSON.stringify(msg) + '<br/>';
} catch (e) {
console.log('Invalid ws frame:', ev.data);
}
};
ws.onclose = function() {
clearTimeout(tid);
tid = setTimeout(reconnect, 1000);
log.innerHTML += 'DISCONNECTED<br/>';
};
ws.onerror = err => console.log(err);
};
reconnect();
};
watch();
</script>
</html>