@@ -10,37 +10,35 @@ addToLibrary({
1010 } ,
1111 $SOCKFS__deps : [ '$FS' ] ,
1212 $SOCKFS : {
13+ #if expectToReceiveOnModule ( 'websocket' )
14+ websocketArgs : { } ,
15+ #endif
16+ callbacks : { } ,
17+ on ( event , callback ) {
18+ SOCKFS . callbacks [ event ] = callback ;
19+ } ,
20+ emit ( event , param ) {
21+ SOCKFS . callbacks [ event ] ?. ( param ) ;
22+ } ,
1323 mount ( mount ) {
14- // If Module['websocket'] has already been defined (e.g. for configuring
15- // the subprotocol/url) use that, if not initialise it to a new object.
16- Module [ 'websocket' ] = ( Module [ 'websocket' ] &&
17- ( 'object' === typeof Module [ 'websocket' ] ) ) ? Module [ 'websocket' ] : { } ;
18-
24+ #if expectToReceiveOnModule ( 'websocket' )
25+ // The incomming Module['websocket'] can be used for configuring
26+ // configuring subprotocol/url, etc
27+ SOCKFS . websocketArgs = { { { makeModuleReceiveExpr ( 'websocket' , '{}' ) } } } ;
1928 // Add the Event registration mechanism to the exported websocket configuration
2029 // object so we can register network callbacks from native JavaScript too.
2130 // For more documentation see system/include/emscripten/emscripten.h
22- Module [ 'websocket' ] . _callbacks = { } ;
23- Module [ 'websocket' ] [ 'on' ] = /** @this {Object} */ function ( event , callback ) {
24- if ( 'function' === typeof callback ) {
25- this . _callbacks [ event ] = callback ;
26- }
27- return this ;
28- } ;
29-
30- Module [ 'websocket' ] . emit = /** @this {Object} */ function ( event , param ) {
31- if ( 'function' === typeof this . _callbacks [ event ] ) {
32- this . _callbacks [ event ] . call ( this , param ) ;
33- }
34- } ;
31+ ( Module [ 'websocket' ] ??= { } ) [ 'on' ] = SOCKFS . on ;
32+ #endif
3533
36- // If debug is enabled register simple default logging callbacks for each Event.
3734#if SOCKET_DEBUG
38- Module [ 'websocket' ] [ 'on' ] ( 'error' , ( error ) => dbg ( 'websocket: error ' + error ) ) ;
39- Module [ 'websocket' ] [ 'on' ] ( 'open' , ( fd ) => dbg ( 'websocket: open fd = ' + fd ) ) ;
40- Module [ 'websocket' ] [ 'on' ] ( 'listen' , ( fd ) => dbg ( 'websocket: listen fd = ' + fd ) ) ;
41- Module [ 'websocket' ] [ 'on' ] ( 'connection' , ( fd ) => dbg ( 'websocket: connection fd = ' + fd ) ) ;
42- Module [ 'websocket' ] [ 'on' ] ( 'message' , ( fd ) => dbg ( 'websocket: message fd = ' + fd ) ) ;
43- Module [ 'websocket' ] [ 'on' ] ( 'close' , ( fd ) => dbg ( 'websocket: close fd = ' + fd ) ) ;
35+ // If debug is enabled register simple default logging callbacks for each Event.
36+ SOCKFS . on ( 'error' , ( error ) => dbg ( 'websocket: error ' + error ) ) ;
37+ SOCKFS . on ( 'open' , ( fd ) => dbg ( 'websocket: open fd = ' + fd ) ) ;
38+ SOCKFS . on ( 'listen' , ( fd ) => dbg ( 'websocket: listen fd = ' + fd ) ) ;
39+ SOCKFS . on ( 'connection' , ( fd ) => dbg ( 'websocket: connection fd = ' + fd ) ) ;
40+ SOCKFS . on ( 'message' , ( fd ) => dbg ( 'websocket: message fd = ' + fd ) ) ;
41+ SOCKFS . on ( 'close' , ( fd ) => dbg ( 'websocket: close fd = ' + fd ) ) ;
4442#endif
4543
4644 return FS . createNode ( null , '/' , { { { cDefs . S_IFDIR } } } | 511 /* 0777 */ , 0 ) ;
@@ -169,36 +167,32 @@ addToLibrary({
169167 } else {
170168 // create the actual websocket object and connect
171169 try {
172- // runtimeConfig gets set to true if WebSocket runtime configuration is available.
173- var runtimeConfig = ( Module [ 'websocket' ] && ( 'object' === typeof Module [ 'websocket' ] ) ) ;
174-
175170 // The default value is 'ws://' the replace is needed because the compiler replaces '//' comments with '#'
176171 // comments without checking context, so we'd end up with ws:#, the replace swaps the '#' for '//' again.
177172 var url = '{{{ WEBSOCKET_URL }}}' . replace ( '#' , '//' ) ;
173+ // Make the WebSocket subprotocol (Sec-WebSocket-Protocol) default to binary if no configuration is set.
174+ var subProtocols = '{{{ WEBSOCKET_SUBPROTOCOL }}}' ; // The default value is 'binary'
175+ // The default WebSocket options
176+ var opts = undefined ;
178177
179- if ( runtimeConfig ) {
180- if ( 'string' === typeof Module [ 'websocket' ] [ 'url' ] ) {
181- url = Module [ 'websocket' ] [ ' url'] ; // Fetch runtime WebSocket URL config.
182- }
178+ # if expectToReceiveOnModule ( 'websocket' )
179+ // Fetch runtime WebSocket URL config.
180+ if ( SOCKFS . websocketArgs [ ' url'] ) {
181+ url = SOCKFS . websocketArgs [ 'url' ] ;
183182 }
183+ // Fetch runtime WebSocket subprotocol config.
184+ if ( SOCKFS . websocketArgs [ 'subprotocol' ] ) {
185+ subProtocols = SOCKFS . websocketArgs [ 'subprotocol' ] ;
186+ } else if ( SOCKFS . websocketArgs [ 'subprotocol' ] === null ) {
187+ subProtocols = 'null'
188+ }
189+ #endif
184190
185191 if ( url === 'ws://' || url === 'wss://' ) { // Is the supplied URL config just a prefix, if so complete it.
186192 var parts = addr . split ( '/' ) ;
187193 url = url + parts [ 0 ] + ":" + port + "/" + parts . slice ( 1 ) . join ( '/' ) ;
188194 }
189195
190- // Make the WebSocket subprotocol (Sec-WebSocket-Protocol) default to binary if no configuration is set.
191- var subProtocols = '{{{ WEBSOCKET_SUBPROTOCOL }}}' ; // The default value is 'binary'
192-
193- if ( runtimeConfig ) {
194- if ( 'string' === typeof Module [ 'websocket' ] [ 'subprotocol' ] ) {
195- subProtocols = Module [ 'websocket' ] [ 'subprotocol' ] ; // Fetch runtime WebSocket subprotocol config.
196- }
197- }
198-
199- // The default WebSocket options
200- var opts = undefined ;
201-
202196 if ( subProtocols !== 'null' ) {
203197 // The regex trims the string (removes spaces at the beginning and end, then splits the string by
204198 // <any space>,<any space> into an Array. Whitespace removal is important for Websockify and ws.
@@ -207,12 +201,6 @@ addToLibrary({
207201 opts = subProtocols ;
208202 }
209203
210- // some webservers (azure) does not support subprotocol header
211- if ( runtimeConfig && null === Module [ 'websocket' ] [ 'subprotocol' ] ) {
212- subProtocols = 'null' ;
213- opts = undefined ;
214- }
215-
216204#if SOCKET_DEBUG
217205 dbg ( 'websocket: connect: ' + url + ', ' + subProtocols . toString ( ) ) ;
218206#endif
@@ -280,8 +268,8 @@ addToLibrary({
280268 dbg ( 'websocket: handle open' ) ;
281269#endif
282270
283- Module [ 'websocket' ] . emit ( 'open' , sock . stream . fd ) ;
284271 sock . connecting = false ;
272+ SOCKFS . emit ( 'open' , sock . stream . fd ) ;
285273
286274 try {
287275 var queued = peer . msg_send_queue . shift ( ) ;
@@ -334,7 +322,7 @@ addToLibrary({
334322 }
335323
336324 sock . recv_queue . push ( { addr : peer . addr , port : peer . port , data : data } ) ;
337- Module [ 'websocket' ] . emit ( 'message' , sock . stream . fd ) ;
325+ SOCKFS . emit ( 'message' , sock . stream . fd ) ;
338326 } ;
339327
340328 if ( ENVIRONMENT_IS_NODE ) {
@@ -346,21 +334,21 @@ addToLibrary({
346334 handleMessage ( ( new Uint8Array ( data ) ) . buffer ) ; // copy from node Buffer -> ArrayBuffer
347335 } ) ;
348336 peer . socket . on ( 'close' , function ( ) {
349- Module [ 'websocket' ] . emit ( 'close' , sock . stream . fd ) ;
337+ SOCKFS . emit ( 'close' , sock . stream . fd ) ;
350338 } ) ;
351339 peer . socket . on ( 'error' , function ( error ) {
352340 // Although the ws library may pass errors that may be more descriptive than
353341 // ECONNREFUSED they are not necessarily the expected error code e.g.
354342 // ENOTFOUND on getaddrinfo seems to be node.js specific, so using ECONNREFUSED
355343 // is still probably the most useful thing to do.
356344 sock . error = { { { cDefs . ECONNREFUSED } } } ; // Used in getsockopt for SOL_SOCKET/SO_ERROR test.
357- Module [ 'websocket' ] . emit ( 'error' , [ sock . stream . fd , sock . error , 'ECONNREFUSED: Connection refused' ] ) ;
345+ SOCKFS . emit ( 'error' , [ sock . stream . fd , sock . error , 'ECONNREFUSED: Connection refused' ] ) ;
358346 // don't throw
359347 } ) ;
360348 } else {
361349 peer . socket . onopen = handleOpen ;
362350 peer . socket . onclose = function ( ) {
363- Module [ 'websocket' ] . emit ( 'close' , sock . stream . fd ) ;
351+ SOCKFS . emit ( 'close' , sock . stream . fd ) ;
364352 } ;
365353 peer . socket . onmessage = function peer_socket_onmessage ( event ) {
366354 handleMessage ( event . data ) ;
@@ -369,7 +357,7 @@ addToLibrary({
369357 // The WebSocket spec only allows a 'simple event' to be thrown on error,
370358 // so we only really know as much as ECONNREFUSED.
371359 sock . error = { { { cDefs . ECONNREFUSED } } } ; // Used in getsockopt for SOL_SOCKET/SO_ERROR test.
372- Module [ 'websocket' ] . emit ( 'error' , [ sock . stream . fd , sock . error , 'ECONNREFUSED: Connection refused' ] ) ;
360+ SOCKFS . emit ( 'error' , [ sock . stream . fd , sock . error , 'ECONNREFUSED: Connection refused' ] ) ;
373361 } ;
374362 }
375363 } ,
@@ -525,7 +513,7 @@ addToLibrary({
525513 port : sock . sport
526514 // TODO support backlog
527515 } ) ;
528- Module [ 'websocket' ] . emit ( 'listen' , sock . stream . fd ) ; // Send Event with listen fd.
516+ SOCKFS . emit ( 'listen' , sock . stream . fd ) ; // Send Event with listen fd.
529517
530518 sock . server . on ( 'connection' , function ( ws ) {
531519#if SOCKET_DEBUG
@@ -541,17 +529,17 @@ addToLibrary({
541529
542530 // push to queue for accept to pick up
543531 sock . pending . push ( newsock ) ;
544- Module [ 'websocket' ] . emit ( 'connection' , newsock . stream . fd ) ;
532+ SOCKFS . emit ( 'connection' , newsock . stream . fd ) ;
545533 } else {
546534 // create a peer on the listen socket so calling sendto
547535 // with the listen socket and an address will resolve
548536 // to the correct client
549537 SOCKFS . websocket_sock_ops . createPeer ( sock , ws ) ;
550- Module [ 'websocket' ] . emit ( 'connection' , sock . stream . fd ) ;
538+ SOCKFS . emit ( 'connection' , sock . stream . fd ) ;
551539 }
552540 } ) ;
553541 sock . server . on ( 'close' , function ( ) {
554- Module [ 'websocket' ] . emit ( 'close' , sock . stream . fd ) ;
542+ SOCKFS . emit ( 'close' , sock . stream . fd ) ;
555543 sock . server = null ;
556544 } ) ;
557545 sock . server . on ( 'error' , function ( error ) {
@@ -562,7 +550,7 @@ addToLibrary({
562550 // occur in a well written app as errors should get trapped in the compiled
563551 // app's own getaddrinfo call.
564552 sock . error = { { { cDefs . EHOSTUNREACH } } } ; // Used in getsockopt for SOL_SOCKET/SO_ERROR test.
565- Module [ 'websocket' ] . emit ( 'error' , [ sock . stream . fd , sock . error , 'EHOSTUNREACH: Host is unreachable' ] ) ;
553+ SOCKFS . emit ( 'error' , [ sock . stream . fd , sock . error , 'EHOSTUNREACH: Host is unreachable' ] ) ;
566554 // don't throw
567555 } ) ;
568556#endif // ENVIRONMENT_MAY_BE_NODE
@@ -763,7 +751,7 @@ addToLibrary({
763751 // FIXME(sbc): This has no corresponding Pop so will currently keep the
764752 // runtime alive indefinitely.
765753 { { { runtimeKeepalivePush ( ) } } }
766- Module [ 'websocket' ] [ 'on' ] ( event , callback ? _callback : null ) ;
754+ SOCKFS . on ( event , callback ? _callback : null ) ;
767755 } ,
768756 emscripten_set_socket_error_callback__deps : [ '$_setNetworkCallback' ] ,
769757 emscripten_set_socket_error_callback : ( userData , callback ) => {
0 commit comments