|
| 1 | +/* Test server for autobahn, run with ASAN. /exit route shuts down. */ |
| 2 | + |
| 3 | +const uWS = require('../dist/uws.js'); |
| 4 | +let listenSocket, listenSocketSSL; |
| 5 | + |
| 6 | +/* todo: A good idea is to test compression 1 for non-SSL and 2 for SSL */ |
| 7 | +/* or really just test more than twice */ |
| 8 | + |
| 9 | +/* Shared, among SSL and non-SSL, behavior of WebSockets */ |
| 10 | +const wsBehavior = { |
| 11 | + /* Options */ |
| 12 | + compression: 2, |
| 13 | + maxPayloadLength: 16 * 1024 * 1024, |
| 14 | + idleTimeout: 60, |
| 15 | + /* Handlers */ |
| 16 | + open: (ws, req) => { |
| 17 | + console.log('A WebSocket connected via URL: ' + req.getUrl() + '!'); |
| 18 | + }, |
| 19 | + message: (ws, message, isBinary) => { |
| 20 | + ws.send(message, isBinary); |
| 21 | + }, |
| 22 | + drain: (ws) => { |
| 23 | + console.log('WebSocket backpressure: ' + ws.getBufferedAmount()); |
| 24 | + }, |
| 25 | + close: (ws, code, message) => { |
| 26 | + console.log('WebSocket closed'); |
| 27 | + } |
| 28 | +}; |
| 29 | + |
| 30 | +/* The SSL test server is on port 9002 */ |
| 31 | +const sslApp = uWS.SSLApp({ |
| 32 | + key_file_name: 'misc/key.pem', |
| 33 | + cert_file_name: 'misc/cert.pem', |
| 34 | + passphrase: '1234' |
| 35 | +}).ws('/*', wsBehavior).any('/exit', (res, req) => { |
| 36 | + /* Stop listening on both SSL and non-SSL */ |
| 37 | + if (listenSocket) { |
| 38 | + uWS.us_listen_socket_close(listenSocket); |
| 39 | + uWS.us_listen_socket_close(listenSocketSSL); |
| 40 | + listenSocket = null; |
| 41 | + } |
| 42 | + |
| 43 | + /* Close this connection */ |
| 44 | + res.close(); |
| 45 | +}).listen(9002, (token) => { |
| 46 | + if (token) { |
| 47 | + listenSocketSSL = token; |
| 48 | + console.log('SSL test server is up...'); |
| 49 | + } |
| 50 | +}); |
| 51 | + |
| 52 | +/* The non-SSL test server is on port 9001 */ |
| 53 | +const app = uWS.App().ws('/*', wsBehavior).any('/exit', (res, req) => { |
| 54 | + /* Stop listening on both SSL and non-SSL */ |
| 55 | + if (listenSocket) { |
| 56 | + uWS.us_listen_socket_close(listenSocket); |
| 57 | + uWS.us_listen_socket_close(listenSocketSSL); |
| 58 | + listenSocket = null; |
| 59 | + } |
| 60 | + |
| 61 | + /* Close this connection */ |
| 62 | + res.close(); |
| 63 | +}).listen(9001, (token) => { |
| 64 | + if (token) { |
| 65 | + listenSocket = token; |
| 66 | + console.log('Non-SSL test server is up...'); |
| 67 | + } |
| 68 | +}); |
| 69 | + |
| 70 | +/* This is required to check for memory leaks */ |
| 71 | +process.on('beforeExit', () => { |
| 72 | + app.forcefully_free(); |
| 73 | + sslApp.forcefully_free(); |
| 74 | +}); |
0 commit comments