@@ -10,37 +10,35 @@ addToLibrary({
10
10
} ,
11
11
$SOCKFS__deps : [ '$FS' ] ,
12
12
$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
+ } ,
13
23
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' , '{}' ) } } } ;
19
28
// Add the Event registration mechanism to the exported websocket configuration
20
29
// object so we can register network callbacks from native JavaScript too.
21
30
// 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
35
33
36
- // If debug is enabled register simple default logging callbacks for each Event.
37
34
#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 ) ) ;
44
42
#endif
45
43
46
44
return FS . createNode ( null , '/' , { { { cDefs . S_IFDIR } } } | 511 /* 0777 */ , 0 ) ;
@@ -169,36 +167,32 @@ addToLibrary({
169
167
} else {
170
168
// create the actual websocket object and connect
171
169
try {
172
- // runtimeConfig gets set to true if WebSocket runtime configuration is available.
173
- var runtimeConfig = ( Module [ 'websocket' ] && ( 'object' === typeof Module [ 'websocket' ] ) ) ;
174
-
175
170
// The default value is 'ws://' the replace is needed because the compiler replaces '//' comments with '#'
176
171
// comments without checking context, so we'd end up with ws:#, the replace swaps the '#' for '//' again.
177
172
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 ;
178
177
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' ] ;
183
182
}
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
184
190
185
191
if ( url === 'ws://' || url === 'wss://' ) { // Is the supplied URL config just a prefix, if so complete it.
186
192
var parts = addr . split ( '/' ) ;
187
193
url = url + parts [ 0 ] + ":" + port + "/" + parts . slice ( 1 ) . join ( '/' ) ;
188
194
}
189
195
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
-
202
196
if ( subProtocols !== 'null' ) {
203
197
// The regex trims the string (removes spaces at the beginning and end, then splits the string by
204
198
// <any space>,<any space> into an Array. Whitespace removal is important for Websockify and ws.
@@ -207,12 +201,6 @@ addToLibrary({
207
201
opts = subProtocols ;
208
202
}
209
203
210
- // some webservers (azure) does not support subprotocol header
211
- if ( runtimeConfig && null === Module [ 'websocket' ] [ 'subprotocol' ] ) {
212
- subProtocols = 'null' ;
213
- opts = undefined ;
214
- }
215
-
216
204
#if SOCKET_DEBUG
217
205
dbg ( 'websocket: connect: ' + url + ', ' + subProtocols . toString ( ) ) ;
218
206
#endif
@@ -280,8 +268,8 @@ addToLibrary({
280
268
dbg ( 'websocket: handle open' ) ;
281
269
#endif
282
270
283
- Module [ 'websocket' ] . emit ( 'open' , sock . stream . fd ) ;
284
271
sock . connecting = false ;
272
+ SOCKFS . emit ( 'open' , sock . stream . fd ) ;
285
273
286
274
try {
287
275
var queued = peer . msg_send_queue . shift ( ) ;
@@ -334,7 +322,7 @@ addToLibrary({
334
322
}
335
323
336
324
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 ) ;
338
326
} ;
339
327
340
328
if ( ENVIRONMENT_IS_NODE ) {
@@ -346,21 +334,21 @@ addToLibrary({
346
334
handleMessage ( ( new Uint8Array ( data ) ) . buffer ) ; // copy from node Buffer -> ArrayBuffer
347
335
} ) ;
348
336
peer . socket . on ( 'close' , function ( ) {
349
- Module [ 'websocket' ] . emit ( 'close' , sock . stream . fd ) ;
337
+ SOCKFS . emit ( 'close' , sock . stream . fd ) ;
350
338
} ) ;
351
339
peer . socket . on ( 'error' , function ( error ) {
352
340
// Although the ws library may pass errors that may be more descriptive than
353
341
// ECONNREFUSED they are not necessarily the expected error code e.g.
354
342
// ENOTFOUND on getaddrinfo seems to be node.js specific, so using ECONNREFUSED
355
343
// is still probably the most useful thing to do.
356
344
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' ] ) ;
358
346
// don't throw
359
347
} ) ;
360
348
} else {
361
349
peer . socket . onopen = handleOpen ;
362
350
peer . socket . onclose = function ( ) {
363
- Module [ 'websocket' ] . emit ( 'close' , sock . stream . fd ) ;
351
+ SOCKFS . emit ( 'close' , sock . stream . fd ) ;
364
352
} ;
365
353
peer . socket . onmessage = function peer_socket_onmessage ( event ) {
366
354
handleMessage ( event . data ) ;
@@ -369,7 +357,7 @@ addToLibrary({
369
357
// The WebSocket spec only allows a 'simple event' to be thrown on error,
370
358
// so we only really know as much as ECONNREFUSED.
371
359
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' ] ) ;
373
361
} ;
374
362
}
375
363
} ,
@@ -525,7 +513,7 @@ addToLibrary({
525
513
port : sock . sport
526
514
// TODO support backlog
527
515
} ) ;
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.
529
517
530
518
sock . server . on ( 'connection' , function ( ws ) {
531
519
#if SOCKET_DEBUG
@@ -541,17 +529,17 @@ addToLibrary({
541
529
542
530
// push to queue for accept to pick up
543
531
sock . pending . push ( newsock ) ;
544
- Module [ 'websocket' ] . emit ( 'connection' , newsock . stream . fd ) ;
532
+ SOCKFS . emit ( 'connection' , newsock . stream . fd ) ;
545
533
} else {
546
534
// create a peer on the listen socket so calling sendto
547
535
// with the listen socket and an address will resolve
548
536
// to the correct client
549
537
SOCKFS . websocket_sock_ops . createPeer ( sock , ws ) ;
550
- Module [ 'websocket' ] . emit ( 'connection' , sock . stream . fd ) ;
538
+ SOCKFS . emit ( 'connection' , sock . stream . fd ) ;
551
539
}
552
540
} ) ;
553
541
sock . server . on ( 'close' , function ( ) {
554
- Module [ 'websocket' ] . emit ( 'close' , sock . stream . fd ) ;
542
+ SOCKFS . emit ( 'close' , sock . stream . fd ) ;
555
543
sock . server = null ;
556
544
} ) ;
557
545
sock . server . on ( 'error' , function ( error ) {
@@ -562,7 +550,7 @@ addToLibrary({
562
550
// occur in a well written app as errors should get trapped in the compiled
563
551
// app's own getaddrinfo call.
564
552
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' ] ) ;
566
554
// don't throw
567
555
} ) ;
568
556
#endif // ENVIRONMENT_MAY_BE_NODE
@@ -763,7 +751,7 @@ addToLibrary({
763
751
// FIXME(sbc): This has no corresponding Pop so will currently keep the
764
752
// runtime alive indefinitely.
765
753
{ { { runtimeKeepalivePush ( ) } } }
766
- Module [ 'websocket' ] [ 'on' ] ( event , callback ? _callback : null ) ;
754
+ SOCKFS . on ( event , callback ? _callback : null ) ;
767
755
} ,
768
756
emscripten_set_socket_error_callback__deps : [ '$_setNetworkCallback' ] ,
769
757
emscripten_set_socket_error_callback : ( userData , callback ) => {
0 commit comments