mirror of
https://github.com/uNetworking/uWebSockets.js.git
synced 2025-12-19 18:10:26 -05:00
44 lines
1.0 KiB
JavaScript
Vendored
44 lines
1.0 KiB
JavaScript
Vendored
/* SSL/non-SSL example with async/await functions */
|
|
|
|
function delay(t, val) {
|
|
return new Promise(function(resolve) {
|
|
setTimeout(function() {
|
|
resolve(val);
|
|
}, t);
|
|
});
|
|
}
|
|
|
|
async function someAsyncTask() {
|
|
return delay(500, 'Hey wait for me!');
|
|
}
|
|
|
|
const uWS = require('../dist/uws.js');
|
|
const port = 9001;
|
|
|
|
const app = uWS./*SSL*/App({
|
|
key_file_name: 'misc/key.pem',
|
|
cert_file_name: 'misc/cert.pem',
|
|
passphrase: '1234'
|
|
}).get('/*', async (res) => {
|
|
/* Can't return or yield from here without responding or attaching an abort handler */
|
|
res.onAborted(() => {
|
|
res.aborted = true;
|
|
});
|
|
|
|
/* Awaiting will yield and effectively return to C++, so you need to have called onAborted */
|
|
let r = await someAsyncTask();
|
|
|
|
/* If we were aborted, you cannot respond */
|
|
if (!res.aborted) {
|
|
res.cork(() => {
|
|
res.end(r);
|
|
});
|
|
}
|
|
}).listen(port, (token) => {
|
|
if (token) {
|
|
console.log('Listening to port ' + port);
|
|
} else {
|
|
console.log('Failed to listen to port ' + port);
|
|
}
|
|
});
|