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
/
Copy pathindex.html
160 lines (141 loc) · 4.13 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<!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/vue"></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;
}
.navbar {
background-color: transparent;
}
.columns {
height: 100vh;
}
.messages-column {
max-height: 80vh;
overflow-y: auto;
}
::-webkit-scrollbar {
width: 0px;
}
.title {
font-size: 4rem; !important
}
ul {
padding: 0rem 1rem 0rem 1rem;
}
li {
margin-top: 0.3rem;
}
li:first-child {
margin-top:0;
}
</style>
</head>
<body>
<div id="app">
<!-- simple navbar instead of title -->
<nav class="navbar is-hidden-mobile" role="navigation" aria-label="main navigation">
<div class="navbar-brand">
<div class="navbar-item">
SSE Messages
</div>
</div>
<div id="appNavBar" class="navbar-menu">
<div class="navbar-start">
</div>
<div class="navbar-end">
<div class="navbar-item">
{{ status }}
</div>
</div>
</div>
</nav>
<div class="columns is-vcentered">
<div class="column is-5 has-text-centered">
<h1 class="title">
Messages Received
</h1>
<h2 class="subtitle">
using ServerSentEvents
</h2>
</div>
<div class="column messages-column" ref="messages-column">
<ul>
<li v-for="message in messages">
<article class="message is-info is-small">
<div class="message-body">
<div class="field is-grouped is-grouped-multiline is-pulled-right">
<div class="control">
<div class="tags has-addons">
<span class="tag is-light">{{ formatDate(message.when) }}</span>
<span class="tag is-info">{{ formatTime(message.when) }}</span>
</div>
</div>
</div>
{{message.message}}
</div>
</article>
</li>
</ul>
</div>
</div>
</div>
<script>
var app = new Vue({
el: '#app',
data: {
status: 'Offline',
messages: []
},
methods: {
formatDate: function(date) {
return luxon.DateTime.fromISO(date).toLocaleString(luxon.DateTime.DATE_SHORT);
},
formatTime: function(date) {
return luxon.DateTime.fromISO(date).toLocaleString(luxon.DateTime.TIME_SIMPLE);
},
addMessage: function(message) {
app.messages.push(JSON.parse(message.data));
// waiting for DOM update before scrolling down:
setTimeout(
() => {
this.$refs['messages-column'].scroll(
{
top: this.$refs['messages-column'].scrollHeight,
left: 0,
behavior: 'smooth'
}
)
},
100
);
}
},
created: function() {
const eventSource = new EventSource('http://localhost:5001/events');
this.status = 'Connecting...';
eventSource.onopen = function() {
app.status = 'Connected'
}
eventSource.onmessage = function(message) {
console.log(message);
app.addMessage(message);
}
eventSource.onerror = function(error) {
app.status = 'Connecting...'
}
}
})
</script>
</body>
</html>