-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhome.js
168 lines (144 loc) · 5.66 KB
/
home.js
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
161
162
163
164
165
166
167
168
// ============================================================================|
/*
* Project : HEARIT.IO
*
* Developing an innovative connected/smart home intelligent
* management system for the needs of blind or visually impaired
* persons.
*
* Purpose:
*
* A foront end part of a SSE example including page rendering and a
* SSE clinet implementation (used in a similar way in https://app.hearit.io).
*
* Author: Emil Usunov, hearit.io
*
* License: MIT
*
*/
// ============================================================================|
'use strict'
const {
pingIntervalMs
} = require('./constants')
// ----------------------------------------------------------------------------|
// A fastify route handler for GET '/'
// ----------------------------------------------------------------------------|
module.exports = function (request, reply) {
reply.type('text/html').send(render('/sse', pingIntervalMs))
}
// ----------------------------------------------------------------------------|
// Renders a page and starts a SSE clinet for a given sseUrl.
// Displays SSE messages and a connection status.
// Reconnects to SSE on a user activity on the page.
// ----------------------------------------------------------------------------|
function render (sseUrl = '/sse', pingIntervalMs = 10000) {
const page = String.raw`
<!DOCTYPE html>
<html>
<head>
<script type="module" async>
const userActivityEventList = ['mousedown', 'click', 'focus',
'blur', 'keydown', 'keypressed', 'resize', 'scroll',
'touchstart', 'touchend']
// ----------------------------------------------------------------------|
// A frontend SSE implementation
// ----------------------------------------------------------------------|
class SsePushService {
constructor (url = '/sse', pingIntervalMs = 30000) {
this.url_ = url
this.es_ = undefined
this.pingIntervalMs_ = pingIntervalMs
this.lastEventId_ = undefined
this.idElem_ = document.getElementById('id')
this.dataElem_ = document.getElementById('data')
this.statusElem_ = document.getElementById('status')
}
// --------------------------------------------------------------------|
// Starts a service
// --------------------------------------------------------------------|
start () {
let url = this.url_
if (typeof this.lastEventId_ !== typeof undefined &&
this.lastEventId_.length > 0) {
url += '?id=' + this.lastEventId_
}
this.es_ = new EventSource(url, {withCredentials: true})
this.es_.addEventListener('open', this.sseEventHandler_.bind(this))
this.es_.addEventListener('error', this.sseEventHandler_.bind(this))
this.es_.addEventListener('message', this.sseEventHandler_.bind(this))
this.es_.addEventListener('ping', this.sseEventHandler_.bind(this))
this.es_.addEventListener('gap', this.sseEventHandler_.bind(this))
}
// --------------------------------------------------------------------|
// Stops a service
// --------------------------------------------------------------------|
stop () {
this.es_.removeEventListener('open', this.sseEventHandler_)
this.es_.removeEventListener('error', this.sseEventHandler_)
this.es_.removeEventListener('message', this.sseEventHandler_)
this.es_.removeEventListener('ping', this.sseEventHandler_)
this.es_.removeEventListener('gap', this.sseEventHandler_)
if (this.es_.readyState !== 'closed') {
this.es_.close()
}
this.es_ = undefined
}
// --------------------------------------------------------------------|
// Enables connect/reconnect on user activity.
// --------------------------------------------------------------------|
connectOnUserActivity () {
for (const activity of userActivityEventList) {
window.addEventListener(activity, (event) => {
if (typeof this.es_ === typeof undefined) {
this.start()
}
})
}
}
// --------------------------------------------------------------------|
// Handles all starndart (open, message, error) and
// named SSE events (ping, gap)
// --------------------------------------------------------------------|
sseEventHandler_ (event) {
const type = event.type
// Display SSE id and message
if (type === 'message') {
this.idElem_.textContent = event.lastEventId
this.dataElem_.textContent = event.data
this.lastEventId_ = event.lastEventId
return
}
// Display SSE status
this.statusElem_.textContent = type.toUpperCase() + ' at ' +
(new Date(Date.now())).toISOString()
// Close a SSE connection
if (type === 'error') {
this.stop()
return
}
// Implement handling of 'open', 'ping' and 'gap' events.
}
}
// Start a service
const sse = new SsePushService('${sseUrl}', ${pingIntervalMs})
sse.start()
sse.connectOnUserActivity()
</script>
</head>
<!-- A HTML to display all related SSE information ------------------------>
<body>
<h1>
<span>SSE Id: </span><span id="id"></span>
</h1>
<h1>
<span>SSE Data: </span><span id="data"></span>
</h1>
<h2>
<span>SSE Status: </span><span id="status"></span>
</h2>
</body>
</html>
`
return page
}