Skip to content

Commit 369b4a5

Browse files
committed
Add UpgradeAsync example, update examples
1 parent d1a2556 commit 369b4a5

6 files changed

Lines changed: 93 additions & 13 deletions

File tree

‎examples/Backpressure.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ const app = uWS./*SSL*/App({
3535
/* We need a slightly higher timeout for this crazy example */
3636
idleTimeout: 60,
3737
/* Handlers */
38-
open: (ws, req) => {
39-
console.log('A WebSocket connected via URL: ' + req.getUrl() + '! Pushing data now!');
38+
open: (ws) => {
39+
console.log('A WebSocket connected!');
4040
/* We begin our example by sending until we have backpressure */
4141
while (ws.getBufferedAmount() < backpressure) {
4242
ws.send("This is a message, let's call it " + messageNumber);

‎examples/Broadcast.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const app = uWS./*SSL*/App({
1414
idleTimeout: 10,
1515

1616
/* Handlers */
17-
open: (ws, req) => {
17+
open: (ws) => {
1818
/* Let this client listen to topic "broadcast" */
1919
ws.subscribe('broadcast');
2020
},

‎examples/PubSub.js‎

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,16 @@ const app = uWS./*SSL*/App({
99
passphrase: '1234'
1010
}).ws('/*', {
1111
/* Options */
12-
compression: 0,
12+
compression: uWS.SHARED_COMPRESSOR,
1313
maxPayloadLength: 16 * 1024 * 1024,
1414
idleTimeout: 10,
15-
// would be nice to have maxBackpressure to automatically close slow receivers
15+
maxBackpressure: 1024,
1616

17-
/* Setting 1: merge messages in one, or keep them as separate WebSocket frames - mergePublishedMessages */
18-
/* Setting 2: compression on/off - cannot have dedicated compressor for pubsub yet */
19-
/* Setting 3: maxBackpressure - when we want to automatically terminate a slow receiver */
20-
/* Setting 4: send to all including us, or not? That's not a setting really just use ws.publish or global uWS.publish */
17+
/* Todo, Setting 1: merge messages in one, or keep them as separate WebSocket frames - mergePublishedMessages */
18+
/* Todo, Setting 4: send to all including us, or not? That's not a setting really just use ws.publish or global uWS.publish */
2119

2220
/* Handlers */
23-
open: (ws, req) => {
21+
open: (ws) => {
2422
/* Let this client listen to all sensor topics */
2523
ws.subscribe('home/sensors/#');
2624
},

‎examples/Upgrade.js‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,23 @@ const app = uWS./*SSL*/App({
99
passphrase: '1234'
1010
}).ws('/*', {
1111
/* Options */
12-
compression: 0,
12+
compression: uWS.SHARED_COMPRESSOR,
1313
maxPayloadLength: 16 * 1024 * 1024,
1414
idleTimeout: 10,
1515
/* Handlers */
1616
upgrade: (res, req, context) => {
1717
console.log('An Http connection wants to become WebSocket, URL: ' + req.getUrl() + '!');
1818

19-
res.upgrade({url: req.getUrl()}, req.getHeader('sec-websocket-key'),
19+
/* This immediately calls open handler, you must not use res after this call */
20+
res.upgrade({
21+
url: req.getUrl()
22+
},
23+
/* Spell these correctly */
24+
req.getHeader('sec-websocket-key'),
2025
req.getHeader('sec-websocket-protocol'),
2126
req.getHeader('sec-websocket-extensions'),
2227
context);
28+
2329
},
2430
open: (ws) => {
2531
console.log('A WebSocket connected with URL: ' + ws.url);

‎examples/UpgradeAsync.js‎

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/* A quite detailed WebSockets upgrade example "async" */
2+
3+
const uWS = require('../dist/uws.js');
4+
const port = 9001;
5+
6+
const app = uWS./*SSL*/App({
7+
key_file_name: 'misc/key.pem',
8+
cert_file_name: 'misc/cert.pem',
9+
passphrase: '1234'
10+
}).ws('/*', {
11+
/* Options */
12+
compression: uWS.SHARED_COMPRESSOR,
13+
maxPayloadLength: 16 * 1024 * 1024,
14+
idleTimeout: 10,
15+
/* Handlers */
16+
upgrade: (res, req, context) => {
17+
console.log('An Http connection wants to become WebSocket, URL: ' + req.getUrl() + '!');
18+
19+
/* Keep track of abortions */
20+
const upgradeAborted = {aborted: false};
21+
22+
/* You MUST copy data out of req here, as req is only valid within this immediate callback */
23+
const url = req.getUrl();
24+
const secWebSocketKey = req.getHeader('sec-websocket-key');
25+
const secWebSocketProtocol = req.getHeader('sec-websocket-protocol');
26+
const secWebSocketExtensions = req.getHeader('sec-websocket-extensions');
27+
28+
/* Simulate doing "async" work before upgrading */
29+
setTimeout(() => {
30+
console.log("We are now done with our async task, let's upgrade the WebSocket!");
31+
32+
if (upgradeAborted.aborted) {
33+
console.log("Ouch! Client disconnected before we could upgrade it!");
34+
/* You must not upgrade now */
35+
return;
36+
}
37+
38+
/* This immediately calls open handler, you must not use res after this call */
39+
res.upgrade({
40+
url: url
41+
},
42+
/* Use our copies here */
43+
secWebSocketKey,
44+
secWebSocketProtocol,
45+
secWebSocketExtensions,
46+
context);
47+
}, 1000);
48+
49+
/* You MUST register an abort handler to know if the upgrade was aborted by peer */
50+
res.onAborted(() => {
51+
/* We can simply signal that we were aborted */
52+
upgradeAborted.aborted = true;
53+
});
54+
},
55+
open: (ws) => {
56+
console.log('A WebSocket connected with URL: ' + ws.url);
57+
},
58+
message: (ws, message, isBinary) => {
59+
/* Ok is false if backpressure was built up, wait for drain */
60+
let ok = ws.send(message, isBinary);
61+
},
62+
drain: (ws) => {
63+
console.log('WebSocket backpressure: ' + ws.getBufferedAmount());
64+
},
65+
close: (ws, code, message) => {
66+
console.log('WebSocket closed');
67+
}
68+
}).any('/*', (res, req) => {
69+
res.end('Nothing to see here!');
70+
}).listen(port, (token) => {
71+
if (token) {
72+
console.log('Listening to port ' + port);
73+
} else {
74+
console.log('Failed to listen to port ' + port);
75+
}
76+
});

‎examples/WebSockets.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const app = uWS./*SSL*/App({
99
passphrase: '1234'
1010
}).ws('/*', {
1111
/* Options */
12-
compression: 0,
12+
compression: uWS.SHARED_COMPRESSOR,
1313
maxPayloadLength: 16 * 1024 * 1024,
1414
idleTimeout: 10,
1515
/* Handlers */

0 commit comments

Comments
 (0)