-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
64 lines (60 loc) · 1.92 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
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>aiohttp background job</title>
<style media="screen">
/* We can put styles in here */
body {
background-color: #F9FAF5;
font-family: sans-serif;
text-align: center;
margin: 1em auto;
max-width: 50em;
}
table {
margin: 1em auto;
}
</style>
</head>
<body>
<h1>Welcome to the aiohttp WebSocket push background job demo</h1>
<p>The table below gets updated by JavaScript each time new data arrives over the WebSocket</p>
<!-- This is the table we will be updating -->
<table>
<thead>
<tr>
<th>Year</th>
<th>Month</th>
<th>Day</th>
<th>Hour</th>
<th>Minute</th>
<th>Second</th>
</tr>
</thead>
<tbody>
<!-- Notice that the id here matches the id in `document.getElementById` -->
<tr id="row-where-the-data-goes"></tr>
</tbody>
</table>
<!-- This is a script tag where we can write JavaScript -->
<script>
// Open a websocket connection (JavaScript makes this super easy)
const ws = new WebSocket('ws://0.0.0.0:8080/ws')
// Tell the websocket connection what to do when it receives a message
ws.onmessage = function (event) {
// This parses the message coming over the websocket as a date, but we could parse it as JSON
const date = new Date(event.data);
// Here we are telling the row-where-the-data-goes what HTML should appear inside
document.getElementById('row-where-the-data-goes').innerHTML = `
<td>${date.getFullYear()}</td>
<td>${date.getMonth()}</td>
<td>${date.getDay()}</td>
<td>${date.getHours()}</td>
<td>${date.getMinutes()}</td>
<td>${date.getSeconds()}</td>
`
}
</script>
</body>
</html>