Add a few more examples
This commit is contained in:
parent
84bcf4c62f
commit
a9ec4fd476
@ -1,5 +1,4 @@
|
||||
/* The stand-alone runtime has uWS namespace already loaded. */
|
||||
var uWS = uWS ? uWS : require('../dist/uws.js');
|
||||
/* Minimal SSL/non-SSL example */
|
||||
|
||||
const port = 9001;
|
||||
|
||||
@ -7,32 +6,8 @@ const app = uWS./*SSL*/App({
|
||||
key_file_name: '/home/alexhultman/key.pem',
|
||||
cert_file_name: '/home/alexhultman/cert.pem',
|
||||
passphrase: '1234'
|
||||
}).get('/static/:param', (res, req) => {
|
||||
res.end('Hello, your url is ' + req.getUrl() + ". The param1 is " + req.getParameter(0));
|
||||
}).get('/hello', (res, req) => {
|
||||
res.end('Hej, du är på url: ' + req.getUrl());
|
||||
}).post('/hello', (res, req) => {
|
||||
// todo: receive the data here!
|
||||
res.end('Thanks for the data!');
|
||||
}).ws('/*', {
|
||||
compression: 0,
|
||||
maxPayloadLength: 16 * 1024 * 1024,
|
||||
open: (ws, req) => {
|
||||
console.log(ws);
|
||||
},
|
||||
message: (ws, message, isBinary) => {
|
||||
ws.send(message, isBinary);
|
||||
|
||||
console.log('BufferedAmount is ' + ws.getBufferedAmount());
|
||||
|
||||
ws.close();
|
||||
},
|
||||
drain: (ws) => {
|
||||
console.log('WebSocket drained');
|
||||
},
|
||||
close: (ws, code, message) => {
|
||||
console.log('WebSocket closed');
|
||||
}
|
||||
}).get('/*', (res, req) => {
|
||||
res.end('Hello World!');
|
||||
}).listen(port, (token) => {
|
||||
if (token) {
|
||||
console.log('Listening to port ' + port);
|
||||
|
27
examples/Router.js
Normal file
27
examples/Router.js
Normal file
@ -0,0 +1,27 @@
|
||||
/* Simple demonstration of some router features */
|
||||
|
||||
const port = 9001;
|
||||
|
||||
const app = uWS./*SSL*/App({
|
||||
key_file_name: '/home/alexhultman/key.pem',
|
||||
cert_file_name: '/home/alexhultman/cert.pem',
|
||||
passphrase: '1234'
|
||||
}).get('/user/agent', (res, req) => {
|
||||
/* Read headers */
|
||||
res.end('Your user agent is: ' + req.getHeader('user-agent') + ' thank you, come again!');
|
||||
}).get('/static/yes', (res, req) => {
|
||||
/* Static match */
|
||||
res.end('This is very static');
|
||||
}).get('/candy/:kind', (res, req) => {
|
||||
/* Parameters */
|
||||
res.end('So you want candy? Have some ' + req.getParameter(0) + '!');
|
||||
}).get('/*', (res, req) => {
|
||||
/* Wildcards - make sure to catch them last */
|
||||
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);
|
||||
}
|
||||
});
|
34
examples/WebSockets.js
Normal file
34
examples/WebSockets.js
Normal file
@ -0,0 +1,34 @@
|
||||
/* A quite detailed WebSockets example */
|
||||
|
||||
const uWS = require('../dist/uws.js');
|
||||
const port = 9001;
|
||||
|
||||
const app = uWS./*SSL*/App({
|
||||
key_file_name: '/home/alexhultman/key.pem',
|
||||
cert_file_name: '/home/alexhultman/cert.pem',
|
||||
passphrase: '1234'
|
||||
}).ws('/*', {
|
||||
/* Options */
|
||||
compression: 0,
|
||||
maxPayloadLength: 16 * 1024 * 1024,
|
||||
/* Handlers */
|
||||
open: (ws, req) => {
|
||||
console.log('A WebSocket connected via URL: ' + req.getUrl() + '!');
|
||||
},
|
||||
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');
|
||||
}
|
||||
}).listen(port, (token) => {
|
||||
if (token) {
|
||||
console.log('Listening to port ' + port);
|
||||
} else {
|
||||
console.log('Failed to listen to port ' + port);
|
||||
}
|
||||
});
|
Loading…
Reference in New Issue
Block a user