diff options
author | Michal Klocek <michal.klocek@qt.io> | 2022-12-09 10:46:23 +0100 |
---|---|---|
committer | Qt Cherry-pick Bot <cherrypick_bot@qt-project.org> | 2022-12-16 14:44:49 +0000 |
commit | ccbe330d6bbf3c6049a57af809b648c2c4a2155e (patch) | |
tree | bf725d3657e31a968e2b4792e833a93d88dcd3c0 /examples/webenginewidgets/push-notifications/server.js | |
parent | 67d028e3dcbdab5ccae00b912ddb9dda7892c81e (diff) |
Fix qdoc warningsv6.5.0-beta1
As JavaScript parsing issue got fixed , the limit was
not changed, meaning there where 4 "unused" warnings
possibilities. In the meantime another change
e6a13d97898f3f4b19f61006d0716ca9e1b9f037
was merged which had 3 qdoc issues, which got therefore
unnoticed.
Fix those issues.
Do not use underscore in example name and rename push_notification
to push-notifications (like qtbase network-chat).
Change-Id: I2635424e24d3b9a0a02c2e6fe8ac383eec95c112
Reviewed-by: Allan Sandfeld Jensen <allan.jensen@qt.io>
(cherry picked from commit cbc622f158a94fea9c6af8d675a8743e98267109)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
Diffstat (limited to 'examples/webenginewidgets/push-notifications/server.js')
-rw-r--r-- | examples/webenginewidgets/push-notifications/server.js | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/examples/webenginewidgets/push-notifications/server.js b/examples/webenginewidgets/push-notifications/server.js new file mode 100644 index 000000000..fc7deb08a --- /dev/null +++ b/examples/webenginewidgets/push-notifications/server.js @@ -0,0 +1,65 @@ +// Copyright (C) 2022 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +const express = require('express'); +const webpush = require('web-push'); + +// setup server +const port = 5000; +const server = express(); +server.use(express.json()); +server.use(express.static('content')); + +// we support only one subscription at the time +var subscription = null; + +// setup vapid keys +const vapidKeys = { + publicKey : + "BNO4fIv439RpvbReeABNlDNiiBD2Maykn7EVnwsPseH7-P5hjnzZLEfnejXVP7Zt6MFoKqKeHm4nV9BHvbgoRPg", + privateKey : "HqhrzsRfG5-SB3j45lyUmV7cYZuy-71r2Bb0tgaOefk" +}; + +// set vapid keys for webpush libs +webpush.setVapidDetails('mailto:push@qt.io', vapidKeys.publicKey, vapidKeys.privateKey); + +// add subscribe route +server.post('/subscribe', (req, res) => { + + // subscription request + subscription = req.body; + const delay = req.headers['ping-time']; + console.log('Got subscription with endpoint: ' + subscription.endpoint); + console.log('Ping delay is at: ' + delay); + + // confirm resource created + res.status(201).json({}); + + // schedule notification + setTimeout(() => { sendNotification(delay) }, delay * 1000); +}); + +// add unsubscribe route +server.post('/unsubscribe', (req, res) => { + console.log('Got unsubscribe with endpoint: ' + req.body.endpoint); + subscription = null; + res.status(201).json({}); +}); + +function sendNotification(delay) +{ + if (!subscription) + return; + + // create payload text + const payload = JSON.stringify({ title : 'Ping !', text : 'Visit qt.io', url : 'www.qt.io' }); + + // send notification + console.log('Sending notification !'); + webpush.sendNotification(subscription, payload).catch(err => console.error(err)); + + // schedule next notification + setTimeout(() => { sendNotification(delay) }, delay * 1000); +} + +server.listen(port, () => console.log(`Push server started at port ${port}`)); |