uWebSockets.js/tests/Autobahn.js

103 lines
2.3 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,
2019-10-09 18:30:50 +00:00
maxBackpressure: 16 * 1024 * 1204,
open: (ws, req) => {
if (settings.pubsub) {
ws.subscribe('broadcast');
}
},
2019-02-10 10:54:44 +00:00
message: (ws, message, isBinary) => {
if (settings.pubsub) {
ws.publish('broadcast', message, isBinary);
} else {
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,
pubsub: false
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,
pubsub: false
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,
pubsub: false
});
/* pub/sub based, non-SSL, non-compression */
listenWithSettings({
port: 9004,
ssl: false,
compression: uWS.DISABLED,
pubsub: true
});
/* pub/sub based, SSL, non-compression */
listenWithSettings({
port: 9005,
ssl: true,
compression: uWS.DISABLED,
pubsub: true
2019-01-23 13:10:17 +00:00
});