Skip to content

Commit 83fabf5

Browse files
committed
add web-worker example
1 parent f58c043 commit 83fabf5

12 files changed

+5970
-5
lines changed

package-lock.json

+2,294
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"name": "otus-real-time-web",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "git+https://github.com/freepad/otus-real-time-web.git"
12+
},
13+
"keywords": [],
14+
"author": "",
15+
"license": "ISC",
16+
"bugs": {
17+
"url": "https://github.com/freepad/otus-real-time-web/issues"
18+
},
19+
"homepage": "https://github.com/freepad/otus-real-time-web#readme",
20+
"devDependencies": {
21+
"webpack": "^5.75.0",
22+
"webpack-cli": "^5.0.1"
23+
}
24+
}

server-sent-event/server.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ function accept(req, res) {
4545

4646
if (!module.parent) {
4747
http.createServer(accept).listen(8080);
48+
console.log('http://localhost:8080')
4849
} else {
4950
exports.accept = accept;
50-
}
51+
}

web-worker/.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
node_modules
2+
**/node_modules
3+
.idea

web-worker/app.js

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
const worker = new Worker("worker.js");
2+
3+
if (window.Notification && Notification.permission !== "denied") {
4+
Notification.requestPermission((status) => {
5+
// status is "granted", if accepted by user
6+
if (status === 'granted') {
7+
const n = new Notification('OTUS', {
8+
body: 'Первое уведомление',
9+
// icon: '/path/to/icon.png' // optional
10+
})
11+
setTimeout(n.close(), 1 * 1000)
12+
}
13+
})
14+
}
15+
16+
document.querySelector('.js-start').addEventListener('click', () => {
17+
worker.postMessage({ type: 'start' })
18+
})
19+
20+
document.querySelector('.js-stop').addEventListener('click', () => {
21+
worker.postMessage({ type: 'stop' })
22+
})
23+
24+
let notification
25+
worker.addEventListener('message', (event) => {
26+
if (event.data.type === 'message') {
27+
const message = event.data.payload
28+
console.log(message)
29+
30+
// закрываем предыдущее уведомление
31+
if (notification) {
32+
notification.close()
33+
}
34+
35+
notification = new Notification(message)
36+
}
37+
})

web-worker/index.html

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport"
6+
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
7+
<meta http-equiv="X-UA-Compatible" content="ie=edge">
8+
<title>OTUS web worker example</title>
9+
</head>
10+
<body>
11+
<button class="js-start">Получать уведомления</button>
12+
<button class="js-stop">Перестать получать уведомления</button>
13+
<script src="app.js" type="module"></script>
14+
</body>
15+
</html>

0 commit comments

Comments
 (0)