1- /* Test server for autobahn, run with ASAN. /exit route shuts down. */
2-
1+ /* Test servers for autobahn, run with ASAN. /exit route shuts down everything */
32const uWS = require ( '../dist/uws.js' ) ;
4- let listenSocket , listenSocketSSL ;
53
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 */
4+ /* Keep track of all apps */
5+ let apps = [ ] ;
6+ let closing = false ;
7+
8+ function listenWithSettings ( settings ) {
9+ /* These are our shared SSL options */
10+ let sslOptions = {
11+ key_file_name : 'misc/key.pem' ,
12+ cert_file_name : 'misc/cert.pem' ,
13+ passphrase : '1234'
14+ } ;
15+
16+ /* Create the app */
17+ let app = settings . ssl ? uWS . App ( sslOptions ) : uWS . SSLApp ( sslOptions ) ;
818
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- } ;
19+ /* Attach our behavior from settings */
20+ app . ws ( '/*' , {
21+ compression : settings . compression ,
22+ maxPayloadLength : 16 * 1024 * 1024 ,
23+ idleTimeout : 60 ,
2924
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- }
25+ message : ( ws , message , isBinary ) => {
26+ ws . send ( message , isBinary ) ;
27+ }
28+ } ) . any ( '/exit' , ( res , req ) => {
29+ /* Shut down everything on this route */
30+ if ( ! closing ) {
31+ apps . forEach ( ( a ) => {
32+ uWS . us_listen_socket_close ( a . listenSocket ) ;
33+ } ) ;
34+ closing = true ;
35+ }
4236
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- }
37+ /* Close this connection */
38+ res . close ( ) ;
39+ } ) . listen ( settings . port , ( listenSocket ) => {
40+ if ( listenSocket ) {
41+ /* Add this app with its listenSocket */
42+ apps . push ( {
43+ app : app ,
44+ listenSocket : listenSocket
45+ } ) ;
46+ console . log ( 'Up and running: ' + JSON . stringify ( settings ) ) ;
47+ } else {
48+ /* Failure here */
49+ console . log ( 'Failed to listen, closing everything now' ) ;
50+ process . exit ( 0 ) ;
51+ }
52+ } ) ;
53+ }
54+
55+ /* non-SSL, non-compression */
56+ listenWithSettings ( {
57+ port : 9001 ,
58+ ssl : false ,
59+ compression : 0
5060} ) ;
5161
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- }
62+ /* SSL, shared compressor */
63+ listenWithSettings ( {
64+ port : 9002 ,
65+ ssl : true ,
66+ compression : 1
67+ } ) ;
6068
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- }
69+ /* non-SSL, dedicated compressor */
70+ listenWithSettings ( {
71+ port : 9003 ,
72+ ssl : false ,
73+ compression : 2
6874} ) ;
6975
7076/* This is required to check for memory leaks */
71- process . on ( 'beforeExit' , ( ) => {
72- app . forcefully_free ( ) ;
73- sslApp . forcefully_free ( ) ;
74- } ) ;
77+ process . on ( 'exit' , ( ) => {
78+ apps . forEach ( ( a ) => {
79+ a . app . forcefully_free ( ) ;
80+ } ) ;
81+ } ) ;
0 commit comments