uWebSockets.js/examples/WebSockets.js

38 lines
976 B
JavaScript
Raw Normal View History

2019-01-17 06:22:31 +00:00
/* A quite detailed WebSockets example */
const uWS = require('../dist/uws.js');
const port = 9001;
const app = uWS./*SSL*/App({
2019-01-17 10:42:53 +00:00
key_file_name: 'misc/key.pem',
cert_file_name: 'misc/cert.pem',
2019-01-17 06:22:31 +00:00
passphrase: '1234'
}).ws('/*', {
/* Options */
compression: uWS.SHARED_COMPRESSOR,
2019-01-17 06:22:31 +00:00
maxPayloadLength: 16 * 1024 * 1024,
2019-01-22 17:50:37 +00:00
idleTimeout: 10,
2019-01-17 06:22:31 +00:00
/* Handlers */
2020-06-05 10:36:36 +00:00
open: (ws) => {
console.log('A WebSocket connected!');
2019-01-17 06:22:31 +00:00
},
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');
}
2019-01-22 17:50:37 +00:00
}).any('/*', (res, req) => {
res.end('Nothing to see here!');
2019-01-17 06:22:31 +00:00
}).listen(port, (token) => {
if (token) {
console.log('Listening to port ' + port);
} else {
console.log('Failed to listen to port ' + port);
}
});