Add UpgradeAsync example, update examples
This commit is contained in:
parent
d1a2556fba
commit
369b4a551f
4
examples/Backpressure.js
vendored
4
examples/Backpressure.js
vendored
@ -35,8 +35,8 @@ const app = uWS./*SSL*/App({
|
||||
/* We need a slightly higher timeout for this crazy example */
|
||||
idleTimeout: 60,
|
||||
/* Handlers */
|
||||
open: (ws, req) => {
|
||||
console.log('A WebSocket connected via URL: ' + req.getUrl() + '! Pushing data now!');
|
||||
open: (ws) => {
|
||||
console.log('A WebSocket connected!');
|
||||
/* We begin our example by sending until we have backpressure */
|
||||
while (ws.getBufferedAmount() < backpressure) {
|
||||
ws.send("This is a message, let's call it " + messageNumber);
|
||||
|
2
examples/Broadcast.js
vendored
2
examples/Broadcast.js
vendored
@ -14,7 +14,7 @@ const app = uWS./*SSL*/App({
|
||||
idleTimeout: 10,
|
||||
|
||||
/* Handlers */
|
||||
open: (ws, req) => {
|
||||
open: (ws) => {
|
||||
/* Let this client listen to topic "broadcast" */
|
||||
ws.subscribe('broadcast');
|
||||
},
|
||||
|
12
examples/PubSub.js
vendored
12
examples/PubSub.js
vendored
@ -9,18 +9,16 @@ const app = uWS./*SSL*/App({
|
||||
passphrase: '1234'
|
||||
}).ws('/*', {
|
||||
/* Options */
|
||||
compression: 0,
|
||||
compression: uWS.SHARED_COMPRESSOR,
|
||||
maxPayloadLength: 16 * 1024 * 1024,
|
||||
idleTimeout: 10,
|
||||
// would be nice to have maxBackpressure to automatically close slow receivers
|
||||
maxBackpressure: 1024,
|
||||
|
||||
/* Setting 1: merge messages in one, or keep them as separate WebSocket frames - mergePublishedMessages */
|
||||
/* Setting 2: compression on/off - cannot have dedicated compressor for pubsub yet */
|
||||
/* Setting 3: maxBackpressure - when we want to automatically terminate a slow receiver */
|
||||
/* Setting 4: send to all including us, or not? That's not a setting really just use ws.publish or global uWS.publish */
|
||||
/* Todo, Setting 1: merge messages in one, or keep them as separate WebSocket frames - mergePublishedMessages */
|
||||
/* Todo, Setting 4: send to all including us, or not? That's not a setting really just use ws.publish or global uWS.publish */
|
||||
|
||||
/* Handlers */
|
||||
open: (ws, req) => {
|
||||
open: (ws) => {
|
||||
/* Let this client listen to all sensor topics */
|
||||
ws.subscribe('home/sensors/#');
|
||||
},
|
||||
|
10
examples/Upgrade.js
vendored
10
examples/Upgrade.js
vendored
@ -9,17 +9,23 @@ const app = uWS./*SSL*/App({
|
||||
passphrase: '1234'
|
||||
}).ws('/*', {
|
||||
/* Options */
|
||||
compression: 0,
|
||||
compression: uWS.SHARED_COMPRESSOR,
|
||||
maxPayloadLength: 16 * 1024 * 1024,
|
||||
idleTimeout: 10,
|
||||
/* Handlers */
|
||||
upgrade: (res, req, context) => {
|
||||
console.log('An Http connection wants to become WebSocket, URL: ' + req.getUrl() + '!');
|
||||
|
||||
res.upgrade({url: req.getUrl()}, req.getHeader('sec-websocket-key'),
|
||||
/* This immediately calls open handler, you must not use res after this call */
|
||||
res.upgrade({
|
||||
url: req.getUrl()
|
||||
},
|
||||
/* Spell these correctly */
|
||||
req.getHeader('sec-websocket-key'),
|
||||
req.getHeader('sec-websocket-protocol'),
|
||||
req.getHeader('sec-websocket-extensions'),
|
||||
context);
|
||||
|
||||
},
|
||||
open: (ws) => {
|
||||
console.log('A WebSocket connected with URL: ' + ws.url);
|
||||
|
76
examples/UpgradeAsync.js
vendored
Normal file
76
examples/UpgradeAsync.js
vendored
Normal file
@ -0,0 +1,76 @@
|
||||
/* A quite detailed WebSockets upgrade example "async" */
|
||||
|
||||
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'
|
||||
}).ws('/*', {
|
||||
/* Options */
|
||||
compression: uWS.SHARED_COMPRESSOR,
|
||||
maxPayloadLength: 16 * 1024 * 1024,
|
||||
idleTimeout: 10,
|
||||
/* Handlers */
|
||||
upgrade: (res, req, context) => {
|
||||
console.log('An Http connection wants to become WebSocket, URL: ' + req.getUrl() + '!');
|
||||
|
||||
/* Keep track of abortions */
|
||||
const upgradeAborted = {aborted: false};
|
||||
|
||||
/* You MUST copy data out of req here, as req is only valid within this immediate callback */
|
||||
const url = req.getUrl();
|
||||
const secWebSocketKey = req.getHeader('sec-websocket-key');
|
||||
const secWebSocketProtocol = req.getHeader('sec-websocket-protocol');
|
||||
const secWebSocketExtensions = req.getHeader('sec-websocket-extensions');
|
||||
|
||||
/* Simulate doing "async" work before upgrading */
|
||||
setTimeout(() => {
|
||||
console.log("We are now done with our async task, let's upgrade the WebSocket!");
|
||||
|
||||
if (upgradeAborted.aborted) {
|
||||
console.log("Ouch! Client disconnected before we could upgrade it!");
|
||||
/* You must not upgrade now */
|
||||
return;
|
||||
}
|
||||
|
||||
/* This immediately calls open handler, you must not use res after this call */
|
||||
res.upgrade({
|
||||
url: url
|
||||
},
|
||||
/* Use our copies here */
|
||||
secWebSocketKey,
|
||||
secWebSocketProtocol,
|
||||
secWebSocketExtensions,
|
||||
context);
|
||||
}, 1000);
|
||||
|
||||
/* You MUST register an abort handler to know if the upgrade was aborted by peer */
|
||||
res.onAborted(() => {
|
||||
/* We can simply signal that we were aborted */
|
||||
upgradeAborted.aborted = true;
|
||||
});
|
||||
},
|
||||
open: (ws) => {
|
||||
console.log('A WebSocket connected with URL: ' + ws.url);
|
||||
},
|
||||
message: (ws, message, isBinary) => {
|
||||
/* Ok is false if backpressure was built up, wait for drain */
|
||||
let ok = ws.send(message, isBinary);
|
||||
},
|
||||
drain: (ws) => {
|
||||
console.log('WebSocket backpressure: ' + ws.getBufferedAmount());
|
||||
},
|
||||
close: (ws, code, message) => {
|
||||
console.log('WebSocket closed');
|
||||
}
|
||||
}).any('/*', (res, req) => {
|
||||
res.end('Nothing to see here!');
|
||||
}).listen(port, (token) => {
|
||||
if (token) {
|
||||
console.log('Listening to port ' + port);
|
||||
} else {
|
||||
console.log('Failed to listen to port ' + port);
|
||||
}
|
||||
});
|
2
examples/WebSockets.js
vendored
2
examples/WebSockets.js
vendored
@ -9,7 +9,7 @@ const app = uWS./*SSL*/App({
|
||||
passphrase: '1234'
|
||||
}).ws('/*', {
|
||||
/* Options */
|
||||
compression: 0,
|
||||
compression: uWS.SHARED_COMPRESSOR,
|
||||
maxPayloadLength: 16 * 1024 * 1024,
|
||||
idleTimeout: 10,
|
||||
/* Handlers */
|
||||
|
Loading…
Reference in New Issue
Block a user