forked from ccoenen/server-sent-events-demo
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.html
47 lines (47 loc) · 1.47 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<html>
<head>
<!--
Server-Sent Events / EventSource DEMO
Claudius Coenen
This is free software. Use, modify and tinker with it however you like!
LICENSED UNDER CC-BY-4.0 http://creativecommons.org/licenses/by/4.0/
-->
<style>
body {
font-size: 3em;
font-family: sans-serif;
}
</style>
</head>
<body>
<p id="output">nothing received so far</p>
<script>
// change this line to match the IP address of your Arduino
var source = new EventSource('http://192.168.0.172/ssedata');
// or, in case you're using the PHP Script, use this line instead.
// var source = new EventSource('source.php');
var outputElement = document.getElementById('output');
var eventCounter = 0;
source.addEventListener('testeventcc', function(e) {
eventCounter++;
outputElement.innerText = e.data + " (" + eventCounter + " Events)";
}, false);
source.addEventListener('arduino', function(e) {
eventCounter++;
outputElement.innerText = e.data + " (" + eventCounter + " Events)";
var inputs = JSON.parse(e.data);
//document.body.style.backgroundColor = inputs.in5 > 0 ? '#ff0000' : '#ffffff';
//document.body.style.color = inputs.in6 > 0 ? 'fuchsia' : 'black';
//outputElement.style.opacity = inputs.A0;
}, false);
source.addEventListener('open', function(e) {
console.log("connected");
}, false);
source.addEventListener('error', function(e) {
console.error(e);
if (e.readyState == EventSource.CLOSED) {
}
}, false);
</script>
</body>
</html>