uWebSockets.js/tests/Autobahn.js

81 lines
1.9 KiB
JavaScript
Raw Normal View History

2019-02-10 10:54:44 +00:00
/* Test servers for autobahn, run with ASAN. /exit route shuts down everything */
2019-01-23 13:10:17 +00:00
const uWS = require('../dist/uws.js');
2019-02-10 10:54:44 +00:00
/* Keep track of all apps */
let apps = [];
let closing = false;
function listenWithSettings(settings) {
/* These are our shared SSL options */
let sslOptions = {
key_file_name: 'misc/key.pem',
cert_file_name: 'misc/cert.pem',
passphrase: '1234'
};
/* Create the app */
let app = settings.ssl ? uWS.SSLApp(sslOptions) : uWS.App(sslOptions);
2019-01-23 13:10:17 +00:00
2019-02-10 10:54:44 +00:00
/* Attach our behavior from settings */
app.ws('/*', {
compression: settings.compression,
maxPayloadLength: 16 * 1024 * 1024,
idleTimeout: 60,
message: (ws, message, isBinary) => {
ws.send(message, isBinary, true);
2019-02-10 10:54:44 +00:00
}
}).any('/exit', (res, req) => {
/* Shut down everything on this route */
if (!closing) {
apps.forEach((a) => {
uWS.us_listen_socket_close(a.listenSocket);
});
closing = true;
}
2019-01-23 13:10:17 +00:00
2019-02-10 10:54:44 +00:00
/* Close this connection */
res.close();
}).listen(settings.port, (listenSocket) => {
if (listenSocket) {
/* Add this app with its listenSocket */
apps.push({
app: app,
listenSocket: listenSocket
});
console.log('Up and running: ' + JSON.stringify(settings));
} else {
/* Failure here */
console.log('Failed to listen, closing everything now');
process.exit(0);
}
});
}
/* non-SSL, non-compression */
listenWithSettings({
port: 9001,
ssl: false,
compression: uWS.DISABLED
2019-01-23 13:10:17 +00:00
});
2019-02-10 10:54:44 +00:00
/* SSL, shared compressor */
listenWithSettings({
port: 9002,
ssl: true,
compression: uWS.SHARED_COMPRESSOR
2019-02-10 10:54:44 +00:00
});
2019-01-23 13:10:17 +00:00
2019-02-10 10:54:44 +00:00
/* non-SSL, dedicated compressor */
listenWithSettings({
port: 9003,
ssl: false,
compression: uWS.DEDICATED_COMPRESSOR
2019-01-23 13:10:17 +00:00
});
/* This is required to check for memory leaks */
2019-02-10 10:54:44 +00:00
process.on('exit', () => {
apps.forEach((a) => {
a.app.forcefully_free();
});
});