@@ -66,6 +66,7 @@ and [old][Old Architecture] RN architectures.
66
66
- [ Enabling Alias module]
67
67
- [ Enabling Rewrite module]
68
68
- [ Enabling WebDAV module]
69
+ - [ Connecting to an Active Server in the Native Layer]
69
70
- [ API Reference] ( #api-reference )
70
71
- [ Server] &mdash ; Represents a server instance.
71
72
- [ constructor()] &mdash ; Creates a new [ Server] instance.
@@ -487,6 +488,50 @@ routes when you create [Server] instance, using `extraConfig` option.
487
488
` ,
488
489
` ` `
489
490
491
+ # ## Connecting to an Active Server in the Native Layer
492
+ [Connecting to an Active Server in the Native Layer]: # connecting-to-an-active-server-in-the-native-layer
493
+
494
+ When this library is used the regular way, the [Lighttpd] server in the native
495
+ layer is launched when the [.start()] method of a [Server] instance is triggered
496
+ on the JavaScript (TypeScript) side, and the native server is terminated when
497
+ the [.stop()] method is called on the JS side. In the JS layer we hold most of
498
+ the server-related information (` hostname` , ` port` , ` fileDir` , _etc._),
499
+ and take care of the high-level server control (_i.e._ the optional
500
+ pause / resume of the server when the app enters background / foreground).
501
+ If JS engine is restarted (or just related JS modules are reloaded) the regular
502
+ course of action is to explictly terminate the active server just before it,
503
+ and to re-create, and re-launch it afterwards. If it is not done, the [Lighttpd]
504
+ server will remain active in the native layer across the JS engine restart,
505
+ and it won' t be possible to launch a new server instance after the restart,
506
+ as the library only supports at most one active [Lighttpd] server, and it
507
+ throws an error if the server launch command arrives to the native layer while
508
+ [Lighttpd] server is already active.
509
+
510
+ However, in response to
511
+ [the ticket #95](https://github.com/birdofpreyru/react-native-static-server/issues/95)
512
+ we provide a way to reuse an active native server across JS engine restarts,
513
+ without restarting the server. To do so you:
514
+ - Use [getActiveServerId()] method to check whether the native server is active
515
+ (if so, this method resolves to a non-_null_ ID).
516
+ - Create a new [Server] instance passing into its [constructor()] that server ID
517
+ as the `id` option, and [STATES]`.ACTIVE` as the `state` option. These options
518
+ (usually omitted when creating a regular [Server] instance) ensure that
519
+ the created [Server] instance is able to communicate with the already running
520
+ native server, and to correctly handle subsequent [.stop()] and [.start()]
521
+ calls. Beside these, it is up-to-you to set all other options to the values
522
+ you need (_i.e._ setting `id`, and `state` just «connects»
523
+ the newly created [Server] instance to the active native server, but it
524
+ does not restore any other information about the server — you should
525
+ restore or redefine it the way you see fit).
526
+
527
+ Note, this way it is possible to create multiple [Server] instances connected
528
+ to the same active native server. As they have the same `id`, they all will
529
+ represent the same server, thus calling [.stop()] and [.start()] commands
530
+ on any of them will operate the same server, and update the states of all
531
+ these JS server instances, without triggering the error related to
532
+ the «at most one active server a time» (though, it has not been
533
+ carefully tested yet).
534
+
490
535
## API Reference
491
536
### Server
492
537
[Server]: #server
@@ -556,9 +601,8 @@ within `options` argument:
556
601
- ` id` & mdash; ** number** & mdash; Optional. Allows to enforce a specific ID,
557
602
used to communicate with the server instance within the Native layer, thus
558
603
it allows to re-connect to an existing native server instance.
559
- See [getActiveServerId()] for details, and also pay attention to set
560
- the correct `state` option value in this case. By default is is assigned
561
- by the library.
604
+ See & laquo; [Connecting to an Active Server in the Native Layer]& raquo;
605
+ for details. By default, an ` id` is selected by the library.
562
606
563
607
- ` nonLocal` & mdash; ** boolean** & mdash; Optional. By default, if ` hostname`
564
608
option was not provided, the server starts at the " ` 127.0.0.1` " (loopback)
@@ -574,10 +618,14 @@ within `options` argument:
574
618
- ` port` & mdash; ** number** & mdash; Optional. The port at which to start the server.
575
619
If 0 (default) an available port will be automatically selected.
576
620
577
- - `state` — [STATES] — Optional. Allows to enforce initial
578
- server state value, which is needed when connecting to an existing
579
- native server instance with help of the `id` option.
580
- By default it is set to `STATES.INACTIVE`.
621
+ - ` state` & mdash; [STATES] & mdash; Optional. Allows to enforce the initial
622
+ server state value, which is necessary [when connecting to an existing
623
+ native server instance][Connecting to an Active Server in the Native Layer].
624
+ Note, it only influence the logic behind subsequent [.start()] and [.stop()]
625
+ calls, _i.e._ the constructor does not attempt to put the server in this
626
+ state, nor does it check the value is consistent with the active server,
627
+ if any, in the native layer. By default, the state is initialized
628
+ to ` STATES.INACTIVE` .
581
629
582
630
- ` stopInBackground` & mdash; ** boolean** & mdash; Optional.
583
631
@@ -837,10 +885,10 @@ if any exist in the Native layer.
837
885
838
886
This function is provided in response to
839
887
[the ticket #95](https://github.com/birdofpreyru/react-native-static-server/issues/95),
840
- which asked to provide a way to re-connect to a server running within the Native
841
- layer after a reset of TypeScript RN layer. The ID returned by this function can
842
- be passed in into [Server] instance [ constructor()] to create server instance
843
- communicating to the existing native layer server.
888
+ to allow «[Connecting to an Active Server in the Native Layer]».
889
+ The ID returned by this function can be passed in into [Server] instance
890
+ [ constructor()] to create server instance communicating to the existing native
891
+ layer server.
844
892
845
893
**NOTE:** It is different from [getActiveServer()] function above, which
846
894
returns the acurrently active, starting, or stopping [Server] instance based on
0 commit comments