-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
33 lines (29 loc) · 1.11 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
<!DOCTYPE html>
<html>
<head>
<title>HTML5 Server Side Event Example in Go</title>
</head>
<body>
Yo {{.}}, here are some facinating messages about the
current time:<br>
<script type="text/javascript">
// Create a new HTML5 EventSource
var hookTypes = ["nowPlaying", "playerTime", "queue"]
for (let htype of hookTypes) {
var source = new EventSource('/api/subscribe?hooktype='+htype);
// Create a callback for when a new message is received.
source.onmessage = function(e) {
// Append the `data` attribute of the message to the DOM.
document.body.innerHTML += htype + ' ' + e.data + '<br>';
console.log(e.data)
};
source.onopen = () => { console.log('subscription open') }
source.onclose = () => { console.log('subscription channel closed') }
window.addEventListener('beforeUnload', () => {
console.log('closing eventsource')
source.close()
})
}
</script>
</body>
</html>