This repository has been archived by the owner on Mar 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
75 lines (68 loc) · 2.33 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>SSE Messages</title>
<script src="https://unpkg.com/jquery"></script>
<script src="https://unpkg.com/luxon"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bulma/0.7.5/css/bulma.min.css">
<style type="text/css">
html {
overflow-y: hidden;
}
body {
background-color: #DDEBE9;
}
.title {
font-size: 3rem;
}
</style>
</head>
<body>
<section class="hero is-success is-bold is-fullheight">
<div class="hero-body">
<div class="container">
<h1 id="title" class="title">
Connecting...
</h1>
<h2 id="subtitle" class="subtitle"></h2>
</div>
</div>
</section>
<script>
const formatDate = function(date) {
let luxonDate = date ? luxon.DateTime.fromISO(date) : luxon.DateTime.local();
return luxonDate.toLocaleString(luxon.DateTime.DATETIME_MED);
};
// get and instance of EventSource connected to our SSE server:
const eventSource = new EventSource('http://localhost:5001/events');
eventSource.onopen = function() {
// we just set some text in title and subtitle:
$('#title').text('Connected!');
$('#subtitle').text(formatDate());
// and change classes for changing background color:
let heroElement = $('.hero');
heroElement.removeClass('is-warning');
heroElement.addClass('is-success');
}
eventSource.onmessage = function(message) {
console.log(message);
// we parse the message to json:
let json = JSON.parse(message.data);
// and set title and subtitle with text and date received:
$('#title').text(json.message);
$('#subtitle').text(formatDate(json.when));
}
eventSource.onerror = function(error) {
// we just set some text in title and subtitle:
$('#title').text('Connecting...');
$('#subtitle').text(formatDate());
// and change classes for changing background color:
let heroElement = $('.hero');
heroElement.removeClass('is-success');
heroElement.addClass('is-warning');
}
</script>
</body>
</html>